使用 if 语句进行时间检查时出现问题 - BASH



>我目前正在检查一个.sh脚本,只是为了做一些基本的事情。

chmod +x catbash.sh
echo 'Hello, Please Enter your User Name'
echo
read VarUserName
currenttime=$(date +%H:%M)
if [[ $currenttime > 11:59 ]] || [[ $currenttime < 12:00 ]]; 
    then echo 'Good Morning' $VarUserName'.'
        fi
if [[ $currenttime > 12:00 ]] || [[ $currenttime < 16:59 ]]; 
    then echo 'Good Afternoon' $VarUserName'.'
        fi
if [[ $currenttime > 17:00 ]] || [[ $currenttime < 19:59 ]]; 
    then echo 'Good Evening' $VarUserName'.'
        fi
if [[ $currenttime > 20:00 ]] || [[ $currenttime < 23:59 ]]; 
    then echo 'Good Night' $VarUserName'.'                                              
        fi

我的问题是我正在尝试使用系统当前时间以在 if 语句中使用,并取决于不同输出的时间。

现在脚本输出所有"好...."回显,并且根据时间不输出单个回声。

谢谢你的帮助。

问题在于您的条件,它们都允许任何给定的值,您是否可能想使用 && 而不是 ||

如果您使用elif结构编写它并从后期开始并逐步向下工作,那会简单得多。

chmod +x catbash.sh
currenttime=$(date +%H:%M)
if [[ "$currenttime" > "19:59" ]]; then echo "Good Night ${USER}"
elif [[ "$currenttime" > "16:59" ]]; then echo "Good Evening ${USER}"
elif [[ "$currenttime" > "11:59" ]]; then echo "Good Afternoon ${USER}"
elif [[ "$currenttime" > "05:59" ]]; then echo "Good Morning ${USER}"
else echo "Good Night ${USER}"

最新更新