高级Bash脚本编程指南(24):时间/日期 命令
成于坚持,败于止步
时间/日期和计时
date
直接调用date命令就会把日期和时间输出到 stdout上. 这个命令有趣的地方在于它的格式化和分析选项上.
root@ubuntu:~/resource/shell-study/0619-2013# date
Wed Jun 19 03:30:04 PDT 2013
root@ubuntu:~/resource/shell-study/0619-2013#
一个使用date命令的实例
#!/bin/bash
echo "The number of days since the year's beginning is `date +%j`."
# 需要在调用格式的前边加上一个'+'号.
# %j用来给出今天是本年度的第几天.
echo "The number of seconds elapsed since 01/01/1970 is `date +%s`."
# %s将产生从"UNIX 元年"到现在为止的秒数,
#+ 但是这东西现在还有用么?
prefix=temp
suffix=$(date +%s) # 'date'命令的"+%s"选项是GNU特性.
filename=$prefix.$suffix
echo $filename
# 这是一种非常好的产生"唯一"临时文件的办法,
#+ 甚至比使用$$都强.
# 如果想了解'date'命令的更多选项, 请查阅这个命令的man页.
exit 0
结果:
root@ubuntu:~/resource/shell-study/0619-2013# chmod +x test1.sh
root@ubuntu:~/resource/shell-study/0619-2013# ./test1.sh
The number of days since the year's beginning is 170.
The number of seconds elapsed since 01/01/1970 is 1371638011.
temp.1371638011
root@ubuntu:~/resource/shell-study/0619-2013#
-u选项将给出UTC时间(Universal Coordinated Time),可以称为世界各地标注时间
root@ubuntu:~/resource/shell-study/0619-2013# date
Wed Jun 19 03:35:30 PDT 2013
root@ubuntu:~/resource/shell-study/0619-2013# date -u
Wed Jun 19 10:35:34 UTC 2013
root@ubuntu:~/resource/shell-study/0619-2013#
date命令有许多的输出选项. 比如%N将以十亿分之一为单位表示当前时间. 这个选项的一个有趣的用法就是用来产生一个6位的随机数.
date +%N | sed -e 's/000$//' -e 's/^0//'
当然, 还有许多其他的选项(请察看man date).
#!/bin/bash
echo `date +%j`
# 显示今天是本年度的第几天(从1月1日开始计算).
echo `date +%k%M`
# 使用24小时的格式来显示当前小时数和分钟数.
# 'TZ'参数允许改变当前的默认时区.
echo `date` # Mon Mar 28 21:42:16 MST 2005
TZ=EST date # Mon Mar 28 23:42:16 EST 2005
echo $TZ
SixDaysAgo=$(date --date='6 days ago')
echo $SixDaysAgo
OneMonthAgo=$(date --date='1 month ago') # 四周前(不是一个月).
echo $OneMonthAgo
OneYearAgo=$(date --date='1 year ago')
echo $OneYearAgo
exit 0
结果:
root@ubuntu:~/resource/shell-study/0619-2013# ./test2.sh
170
349
Wed Jun 19 03:49:57 PDT 2013
Wed Jun 19 05:49:57 EST 2013
Thu Jun 13 03:49:57 PDT 2013
Sun May 19 03:49:57 PDT 2013
Tue Jun 19 03:49:57 PDT 2012
root@ubuntu:~/resource/shell-study/0619-2013#
zdump
时区dump: 查看特定时区的当前时间.
root@ubuntu:~/resource/shell-study/0619-2013# zdump EST
EST Wed Jun 19 05:53:41 2013 EST
root@ubuntu:~/resource/shell-study/0619-2013# zdump PDT
PDT Wed Jun 19 10:53:49 2013 PDT
root@ubuntu:~/resource/shell-study/0619-2013# zdump MST
MST Wed Jun 19 03:54:08 2013 MST
root@ubuntu:~/resource/shell-study/0619-2013# zdump UTC
UTC Wed Jun 19 10:54:24 2013 UTC
root@ubuntu:~/resource/shell-study/0619-2013#
time
输出统计出来的命令执行的时间.
root@ubuntu:~/resource/shell-study/0619-2013# time ls -l
total 8
-rwxr-xr-x 1 root root 639 2013-06-19 03:32 test1.sh
-rwxr-xr-x 1 root root 556 2013-06-19 03:49 test2.sh
real 0m0.011s
user 0m0.004s
sys 0m0.000s
root@ubuntu:~/resource/shell-study/0619-2013#
touch
这是一个用来更新文件被访问或修改的时间的工具, 这个时间可以是当前系统的时间,也可以是指定的时间, 这个命令也用来产生一个新文件. 命令touch file将产生一个file为名字的0字节长度文件, 当然前提是file文件不存在. 为了存储时间信息, 就需要一个时间戳为空的文件, 比如当你想跟踪一个工程的修改时间的时候, 这就非常有用了.
touch命令等价于: >> newfile或>> newfile(对于一个普通文件).
root@ubuntu:~/resource/shell-study/0619-2013# ls
test1.sh test2.sh
root@ubuntu:~/resource/shell-study/0619-2013# touch file
root@ubuntu:~/resource/shell-study/0619-2013# ls -l
total 8
-rw-r--r-- 1 root root 0 2013-06-19 03:58 file
-rwxr-xr-x 1 root root 639 2013-06-19 03:32 test1.sh
-rwxr-xr-x 1 root root 556 2013-06-19 03:49 test2.sh
root@ubuntu:~/resource/shell-study/0619-2013# touch file
root@ubuntu:~/resource/shell-study/0619-2013# ls -l
total 8
-rw-r--r-- 1 root root 0 2013-06-19 03:58 file
-rwxr-xr-x 1 root root 639 2013-06-19 03:32 test1.sh
-rwxr-xr-x 1 root root 556 2013-06-19 03:49 test2.sh
root@ubuntu:~/resource/shell-study/0619-2013#
上面的实例说明如果只想touch file,file已经存在时,虽然没有任何提示,其实是没有更新文件时间戳的
at
at命令是一个作业控制命令, 用来在指定时间点上执行指定的命令集合. 它有点像cron命令, 然而, at命令主要还是用来执行那种一次性执行的命令集合.
at 2pm January 15将会产生提示, 提示你输入需要在这个时间上需要执行的命令序列. 这些命令应该是可以和shell 脚本兼容的, 因为实际上在一个可执行的脚本中, 用户每次只能输入一行. 输入将以Ctl-D结束.
你可以使用-f选项或者使用(<)重定向操作符, 来让at命令从一个文件中读取命令集合. 这个文件其实就一个可执行的的脚本, 虽然它是一个不可交互的脚本. 在文件中包含一个run-parts命令, 对于执行一套不同的脚本来说是非常聪明的做法.
batch
batch作业控制命令与at令的行为很相像, 但是batch命令被用来在系统平均负载量降到.8以下时执行一次性的任务. 与at命令相似的是, 它也可以使用-f选项来从文件中读取命令.
cal
从stdout中输出一个格式比较整齐的日历. 既可以指定当前年度, 也可以指定过去或将来的某个年度.
root@ubuntu:~/resource/shell-study/0619-2013# cal
June 2013
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
root@ubuntu:~/resource/shell-study/0619-2013# cal -3
May 2013 June 2013 July 2013
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 1 1 2 3 4 5 6
5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30
root@ubuntu:~/resource/shell-study/0619-2013# cal -j
June 2013
Su Mo Tu We Th Fr Sa
152
153 154 155 156 157 158 159
160 161 162 163 164 165 166
167 168 169 170 171 172 173
174 175 176 177 178 179 180
181
root@ubuntu:~/resource/shell-study/0619-2013# cal -hJeo 2012
usage: cal [-hjy] [[month] year]
cal [-hj] [-m month] [year]
ncal [-hJjpwy] [-s country_code] [[month] year]
ncal [-hJeo] [year]
root@ubuntu:~/resource/shell-study/0619-2013# ncal -hJeo 2012
April 2 2012
root@ubuntu:~/resource/shell-study/0619-2013# cal -hjy 12 2012
2012
January February
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7 32 33 34 35
8 9 10 11 12 13 14 36 37 38 39 40 41 42
15 16 17 18 19 20 21 43 44 45 46 47 48 49
22 23 24 25 26 27 28 50 51 52 53 54 55 56
29 30 31 57 58 59 60
March April
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
61 62 63 92 93 94 95 96 97 98
64 65 66 67 68 69 70 99 100 101 102 103 104 105
71 72 73 74 75 76 77 106 107 108 109 110 111 112
78 79 80 81 82 83 84 113 114 115 116 117 118 119
85 86 87 88 89 90 91 120 121
May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
122 123 124 125 126 153 154
127 128 129 130 131 132 133 155 156 157 158 159 160 161
134 135 136 137 138 139 140 162 163 164 165 166 167 168
141 142 143 144 145 146 147 169 170 171 172 173 174 175
148 149 150 151 152 176 177 178 179 180 181 182
July August
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
183 184 185 186 187 188 189 214 215 216 217
190 191 192 193 194 195 196 218 219 220 221 222 223 224
197 198 199 200 201 202 203 225 226 227 228 229 230 231
204 205 206 207 208 209 210 232 233 234 235 236 237 238
211 212 213 239 240 241 242 243 244
September October
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
245 275 276 277 278 279 280
246 247 248 249 250 251 252 281 282 283 284 285 286 287
253 254 255 256 257 258 259 288 289 290 291 292 293 294
260 261 262 263 264 265 266 295 296 297 298 299 300 301
267 268 269 270 271 272 273 302 303 304 305
274
November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
306 307 308 336
309 310 311 312 313 314 315 337 338 339 340 341 342 343
316 317 318 319 320 321 322 344 345 346 347 348 349 350
323 324 325 326 327 328 329 351 352 353 354 355 356 357
330 331 332 333 334 335 358 359 360 361 362 363 364
365 366
root@ubuntu:~/resource/shell-study/0619-2013#
对cal相关的一些操作可man一下
sleep
这个命令与一个等待循环的效果一样. 你可以指定需要暂停的秒数, 这段时间将什么都不干. 当一个后台运行的进程需要偶尔检测一个事件时, 这个功能很有用. 也可用于计时
1 sleep 3 # 暂停3秒.
sleep默认是以秒为单位, 但是你也可以指定分钟, 小时, 或者天数为单位.
1 sleep 3 h # 暂停3小时!
如果你想每隔一段时间来运行一个命令的话, 那么watch命令将比sleep命令好得多.
下面是执行watch -n 5 echo hello的效果
usleep
指定需要sleep的微秒数 ("u"会被希腊人读成"mu", 或者是 micro- 前缀). 与上边的sleep命令相同, 但这个命令以微秒为单位. 当需要精确计时, 或者需要非常频繁的监控一个正在运行进程的时候, 这个命令非常有用.
1 usleep 30 # 暂停30微秒.
事实上usleep命令并不能提供非常精确的计时, 所以如果你需要运行一个实时的任务的话, 这个命令并不适合.
hwclock, clock
hwclock命令可以访问或调整硬件时钟. 这个命令的一些选项需要具有root权限. 在系统启动的时候, /etc/rc.d/rc.sysinit, 会使用hwclock来从硬件时钟中读取并设置系统时间.
clock命令与hwclock命令完全相同.
可以使用这个hwclock调整系统时间,比如我的系统时间就是错误的
root@ubuntu:~/resource/shell-study/0619-2013# hwclock
Wed 19 Jun 2013 07:31:28 PM PDT -0.660673 seconds
root@ubuntu:~/resource/shell-study/0619-2013# date
Wed Jun 19 04:30:52 PDT 2013
root@ubuntu:~/resource/shell-study/0619-2013# hwclock -s
root@ubuntu:~/resource/shell-study/0619-2013# date
Wed Jun 19 19:31:44 PDT 2013
root@ubuntu:~/resource/shell-study/0619-2013#
经过调整之后可以看到ok了
先到这里了,O(∩_∩)O~
我的专栏地址:http://blog.csdn.net/column/details/shell-daily-study.html
待续。。。。。