如何在shell脚本中列出每个星期一和星期四的日期?

use*_*507 2 unix shell scripting date

如果我给出月份和年份作为输入,则输出应该是该月的星期一和星期四的日期。

例如:如果输入是年 = 2013 月 = 02 输出应该是

2013-02-04 
2013-02-07 
2013-02-11 
2013-02-14
2013-02-18
2013-02-21 
2013-02-25
2013-02-28
Run Code Online (Sandbox Code Playgroud)

如何在 shell 脚本中使用日期函数?

gle*_*man 5

这个方法只需要调用2次date,剩下的只是算术运算

year=2013
month=2
last=$(date -d "$year-$month-1 + 1 month - 1 day" +%d)
wday=$(date -d "$year-$month-1" +%w)
for (( day=1; day<=$last; day++ )); do
    if (( wday == 1 || wday == 4 )); then
        printf "%4d-%02d-%02d\n" $year $month $day
    fi
    (( wday = (wday + 1) % 7 ))
done
Run Code Online (Sandbox Code Playgroud)
2013-02-04
2013-02-07
2013-02-11
2013-02-14
2013-02-18
2013-02-21
2013-02-25
2013-02-28
Run Code Online (Sandbox Code Playgroud)