今日の日付から7日を引く

今日の日付から7日を引く

これが私のスクリプトです。今日の日付から 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.  

関連情報