从今天的日期减去 7 天

mac*_*ama 3 osx shell-script date

这是我的脚本。我需要从今天的日期中减去 7 天并在文件名中使用它。我正在使用 Mac。

#/bin/bash
DATE=$(date -d "-7 days")
echo $DATE
Run Code Online (Sandbox Code Playgroud)

当我运行这个 .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]
Run Code Online (Sandbox Code Playgroud)

Eri*_*ouf 7

有了BSD date你需要不同的语法:

DATE="$(date -v-7d)"
Run Code Online (Sandbox Code Playgroud)

在我的 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.  
Run Code Online (Sandbox Code Playgroud)