#!/bin/bash
# 练习'date'命令
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时期(UNIX epoch)"开始至今已过的秒数,
# 但是这有什么用呢?
prefix=temp
suffix=$(date +%s) # “日期”的“+%s”选项是GNU特有的。
filename=$prefix.$suffix
echo "Temporary filename = $filename"
# 它非常适合创建 “唯一且随机” 的临时文件名,
# 甚至比使用$$还好.
# 你可以阅读'date' man手册,了解更多格式选项。
exit 0
-u选项给出了UTC时间 (世界标准时间)。
bash$ date
Fri Mar 29 21:07:39 MST 2002
bash$ date -u
Sat Mar 30 04:07:42 UTC 2002
date +%N | sed -e 's/000$//' -e 's/^0//'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# 如果存在,去除前后的零。
# 生成的整数的长度取决于去除了多少个零。
# 115281032
# 63408725
# 394504284
还有远远不止这些选项(尝试执行man date)
date +%j
# 输出今天在当年中的位置(自1月1日以来经过的天数)
date +%k%M
# 作为一个单独的数字字符串,以24小时制输出当前小时和分钟。
# 'TZ'参数允许覆盖默认时区。
date # Mon Mar 28 21:42:16 MST 2005
TZ=EST date # Mon Mar 28 23:42:16 EST 2005
# 感谢Frank Kannemann and Pete Sjoberg的点子.
SixDaysAgo=$(date --date='6 days ago')
OneMonthAgo=$(date --date='1 month ago') # 4周前(不是一个月!)
OneYearAgo=$(date --date='1 year ago')