如何在 Bash Shell 中获取给定年份的星期几的第一天?

How do I get the first day of the given year's day of the week in Bash Shell?

我有一个 bash 脚本,我试图在其中传递年份并获取该年 1 月 1 日那一周的第一天。

我试过使用日期命令:

yearsago=$(( 2021 - year))
date --date="$(date +'%Y-%m-01') - ${yearsago} year "

运气不好。例如,如果我输入年份:2020 作为变量。它应该找到当年的第一天,即 1 月 1 日,并找到 2020 年 1 月 1 日是星期几。

输出将是如果星期一,打印“1”,如果星期二,打印“2” 谢谢。

get the first day of the week of January 1st of that year.

似乎微不足道:

date -d "$year-1-1" +%a

请看下面的代码,它会给你过去一年的第一天:

# !/usr/bin/bash
year=2020
firstday=`date -d "01 Jan $year" +'%A,%d'`
echo "First day of the year '$year' is : '$firstday'"

输出: '2020' 的第一天是:'Wednesday,01'
如果你只想得到 day ,然后删除 %d 选项。

firstday=`date -d "01 Jan $year" +'%A`

输出:'2020' 的第一天是:'Wednesday'