检查shell参数是否为每月的最后一天



我想检查命令行中传递的参数是否是一个月的最后一天,如下所示:./check_date.sh 20210731

如果20210731是月份的最后一天;月的最后一天";else echo";没有月的最后一天";

mydate="$1"
#i found this code in stackoverflow thread but do not now if can take my argument to check weather is the last day or month or not
if [[ $(date -d "+1 day" +%m) != $(date +%m) ]]
then
echo "Today is the last day of the month"
else
echo "Today is NOT the last day of the month"
fi

谢谢

只需检查添加到您的日期$11 day(表示为月份的+%d(是否计算为01:

if [[ $(date -d "$1 + 1 day" +%d) == 01 ]]
then echo "$1 is the last day of month"
else echo "$1 is NOT the last day of month"
fi

最新更新