LoginSignup
1
3

More than 3 years have passed since last update.

過去1週間の日付を出力するシェルスクリプト (Linux, macOS)

Posted at

Linux 動作版

1week.sh
#!/bin/sh

# 7から1までのループ
for N in `seq 7 -1 1`
do
  # 7 days ago から 1 days ago まで
  DATE=`date +%Y-%m-%d --date "${N} days ago"`
  echo $DATE
done

動作確認環境: Ubuntu 20.04 LTS (Focal Fossa) + dash 0.5.10.2 + GNU coreutils 8.30 (date, seq)

$ ./1week.sh 
2020-06-27
2020-06-28
2020-06-29
2020-06-30
2020-07-01
2020-07-02
2020-07-03

macOS 動作版

1week.sh
#!/bin/sh

# 7から1までのループ
for N in `seq 7 -1 1`
do
  # -v-7d から -v-1d まで
  DATE=`date -v-${N}d +%Y-%m-%d`
  echo $DATE
done

動作確認環境: macOS Catalina 10.15 + GNU bash 3.2.57 + BSD date + BSD seq

$ ./1week.sh 
2020-06-27
2020-06-28
2020-06-29
2020-06-30
2020-07-01
2020-07-02
2020-07-03
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3