我正在尝试制作一个bash脚本,该脚本在执行时会根据日期是否在一定范围内运行一组不同的命令。我只关心检查日期和月份。
例如:假设我想要一个脚本,该脚本根据欧洲日历告诉我该季节。我将以以下格式为它提供4段代码:
if date is between 01-03 and 01-06 then
echo "It is spring"
if date is between 01-06 and 01-09 then
echo "It is summer"
if date is between 01-09 and 01-12 then
echo "It is autumn"
if date is between 01-12 and 01-03 then
echo "It is winter"
使用date
命令当然可以实现。我只是不确定如何在有效的公式中使用月和日数字来检查此类范围。
答案 0 :(得分:1)
javac 1.8.0_212
指定一种格式,仅将date +%m
输出月份号,因此您可以在自己的条件下使用该格式:
date
答案 1 :(得分:1)
使用case语句来匹配由固定格式的月份和日期组成的单个字符串。
Google Drive
答案 2 :(得分:0)
我发现,对于我所需要的,我可以简单地估计是本月的上半月还是下半月。我使用此检查成功创建了一个工作脚本:
current_month=$(date +%m)
current_day=$(date +%d)
if (( current_month == 1 )) && (( current_day < 16 ))
then
# January, first half
echo "January, first half"
elif (( current_month == 1 )) && (( current_day >= 16 ))
then
# January, second half
echo "January, second half"
elif (( current_month == 2 )) && (( current_day < 16 ))
then
# February, first half
echo "February, first half"
elif (( current_month == 2 )) && (( current_day >= 16 ))
then
# February, second half
echo "February, second half"
elif (( current_month == 3 )) && (( current_day < 16 ))
then
# March, first half
echo "March, first half"
elif (( current_month == 3 )) && (( current_day >= 16 ))
then
# March, second half
echo "March, second half"
elif (( current_month == 4 )) && (( current_day < 16 ))
then
# April, first half
echo "April, first half"
elif (( current_month == 4 )) && (( current_day >= 16 ))
then
# April, second half
echo "April, second half"
elif (( current_month == 5 )) && (( current_day < 16 ))
then
# May, first half
echo "May, first half"
elif (( current_month == 5 )) && (( current_day >= 16 ))
then
# May, second half
echo "May, second half"
elif (( current_month == 6 )) && (( current_day < 16 ))
then
# June, first half
echo "June, first half"
elif (( current_month == 6 )) && (( current_day >= 16 ))
then
# June, second half
echo "June, second half"
elif (( current_month == 7 )) && (( current_day < 16 ))
then
# July, first half
echo "July, first half"
elif (( current_month == 7 )) && (( current_day >= 16 ))
then
# July, second half
echo "July, second half"
elif (( current_month == 8 )) && (( current_day < 16 ))
then
# August, first half
echo "August, first half"
elif (( current_month == 8 )) && (( current_day >= 16 ))
then
# August, second half
echo "August, second half"
elif (( current_month == 9 )) && (( current_day < 16 ))
then
# September, first half
echo "September, first half"
elif (( current_month == 9 )) && (( current_day >= 16 ))
then
# September, second half
echo "September, second half"
elif (( current_month == 10 )) && (( current_day < 16 ))
then
# October, first half
echo "October, first half"
elif (( current_month == 10 )) && (( current_day >= 16 ))
then
# October, second half
echo "October, second half"
elif (( current_month == 11 )) && (( current_day < 16 ))
then
# November, first half
echo "November, first half"
elif (( current_month == 11 )) && (( current_day >= 16 ))
then
# November, second half
echo "November, second half"
elif (( current_month == 12 )) && (( current_day < 16 ))
then
# December, first half
echo "December, first half"
elif (( current_month == 12 )) && (( current_day >= 16 ))
then
# December, second half
echo "December, second half"
else
# Error
echo "Error: Invalid date"
fi
答案 3 :(得分:0)
这是您发布的答案的简单得多的版本:
read -r current_month current_day <<< $(date '+%B %d')
if (( current_day < 16 ))
then
half=first
else
half=second
fi
echo "$current_month, $half half"
今天的输出:
June, first half
%B
格式的代码提供了本地的当前完整月份名称。使用read
同时获取两个值可以避免罕见的问题,即在月份的最后一天的午夜恰好获取不同月份的日期。
此脚本通过使用该格式字符串并逐段构建要输出的字符串来删除lot of repetition。
十行与103行。