这是我的脚本。我需要从今天的日期中减去 7 天并将其用于文件名。我使用的是 Mac。
#/bin/bash
DATE=$(date -d "-7 days")
echo $DATE
当我运行这个 .sh 脚本时,我得到:
$ /Users/xxxxxxx/xxxxxxxx/dateTest.sh
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
答案1
你BSD date
需要不同的语法:
DATE="$(date -v-7d)"
在我的 FreeBSD 上man date
包括:
-v Adjust (i.e., take the current date and display the result of the
adjustment; not actually set the date) the second, minute, hour,
month day, week day, month or year according to val. If val is
preceded with a plus or minus sign, the date is adjusted forwards
or backwards according to the remaining string, otherwise the
relevant part of the date is set.