Bash:根据日期执行不同的函数

Bash:根据日期执行不同的函数,bash,date,scripting,Bash,Date,Scripting,我试图创建一个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

我试图创建一个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
命令来实现。我只是不知道如何在一个有效的公式中使用月数和日数来检查这些范围。

date+%m
指定了一种格式,该格式只能输出月数,因此您可以在您的条件下使用:

current_month=$(date +%m)
if [[ $current_month > 2 && $current_month < 6 ]]; then
    echo "It is spring"
...
current_month=$(日期+%m)
如果[[$current_month>2&&$current_month<6]];然后
呼应“这是春天”
...

使用case语句匹配由固定格式的月份和日期组成的单个字符串

case $(date +%m-%d) in
  03-2[2-9]|03-3*|0[4-6]-[01]*|06-2[012]) echo "spring" ;;
  06-2[2-9]|06-3*|0[7-9]-[01]*|09-2[012]) echo "summer" ;;
  09-2[2-9]|09-3*|1[012]-[01]*|12-2[012]) echo "autumn" ;;
  12-2[2-9]|12-3*|0[1-3]-[01]*|03-2[012]) echo "winter" ;;
esac

我发现,对于我所需要的,我可以简单地估计是上半月还是下半月。我使用以下检查创建了一个工作脚本:

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
current_month=$(日期+%m)
当前日=$(日期+%d)
如果((当前月份==1))&((当前日期<16))
然后
#一月,上半年
回声“一月,上半年”
elif((当前月份==1))&((当前日期>=16))
然后
#一月,下半年
回声“一月,下半年”
elif((本月==2))和((本月<16))
然后
#二月上半月
回声“二月,上半年”
elif((当前月份==2))和((当前日期>=16))
然后
#二月下旬
回声“二月,下半年”
elif((当前月份==3))&((当前日期<16))
然后
#三月上半月
回声“三月,上半场”
elif((当前月份==3))&&((当前日期>=16))
然后
#三月下旬
回声“三月,下半场”
elif((当前月份==4))&((当前日期<16))
然后
#四月,上半年
回声“四月,上半年”
elif((当前月份==4))&((当前日期>=16))
然后
#四月,下半年
回声“四月,下半年”
elif((本月==5))和((本月<16))
然后
#五月,上半年
回声“五月,上半场”
elif((当前月份==5))&((当前日期>=16))
然后
#五月,下半场
回声“五月,下半场”
elif((本月==6))和((本月<16))
然后
#六月上半年
回声“六月,上半年”
elif((当前月份==6))&((当前日期>=16))
然后
#六月下旬
回声“六月,下半年”
elif((本月==7))和((本月<16))
然后
#七月上半年
回声“七月,上半年”
elif((当前月份==7))&((当前日期>=16))
然后
#七月下旬
回声“七月,下半年”
elif((当前月份==8))&((当前日期<16))
然后
#八月上半年
回声“八月,上半年”
elif((当前月份==8))&((当前日期>=16))
然后
#八月下旬
回声“八月,下半年”
elif((本月==9))和((本月<16))
然后
#九月上半年
回声“九月,上半年”
elif((当前月份==9))和((当前日期>=16))
然后
#九月下旬
回声“九月,下半年”
elif((本月==10))和((本月<16))
然后
#十月上半年
回声“十月,上半年”
elif((当前月份==10))和((当前日期>=16))
然后
#十月下旬
回声“十月,下半年”
elif((本月==11))和((本月<16))
然后
#11月上半年
回声“十一月上半年”
elif((本月=11))&((本日>=16))
然后
#11月,下半年
回声“十一月,下半年”
elif((本月==12))&((本月<16))
然后
#十二月上半年
回声“十二月,上半年”
elif((本月=12))&((本日>=16))
然后
#十二月下旬
回声“十二月,下半年”
其他的
#错误
echo“错误:无效日期”
fi

以下是您发布的答案的简单版本:

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"
%B
格式代码提供本地人当前的完整月份名称。使用
read
同时获取两个值可以避免在每月最后一天的午夜获取不同月份的日期这一罕见问题

此脚本通过使用该格式字符串并逐段构建要输出的字符串来删除


10行对103行。

这一天似乎没有必要(忽略6月1日、9月1日、12月1日和3月1日的重叠);e、 例如,春天似乎是第3、4或5个月的任何一天。为什么不将月份和日期合并成一个数字,并进行比较呢?12月份你需要做一些特别的事情,但这是直截了当的。但是@chepner是正确的,因为一个季节不会在月中开始。这只是一个例子:在我的用例中,我也需要这一天。与您的用例不匹配的示例没有帮助。正如我所说,我需要我的脚本根据每个组合执行不同的代码。我发布的版本对我来说仍然很好。@mircakitsune:我们只能用给出的信息回答问题。当提问者提供部分信息,询问除他们真正需要帮助和/或移动目标之外的其他问题时,这几乎是不可能的。我在回答中演示的技巧大大提高了干性,并且可以比仅仅回音字符串更广泛地使用。
June, first half