2014-01-19 31 views
0

我試圖讓日期正常工作。現在是晚上5點(1700小時),即晚上,但總是在早上說。這是我的代碼在Bash中未正確解析日期

currenthour="$(date "+ %-H")" 
echo $currenthour 
if [ $currenthour -gt '12' ] && [ $currenthour -lt '16' ]; then 
    currenttimeofday="Afternoon" 
elif [ $currenthour -gt '16' ] && [ $currenthour -lt '1' ]; then 
    currenttimeofday="Evening" 
else 
    currenttimeofday="Morning" 
fi 
echo $currenttimeofday 

回答

1

您需要刪除加號和百分比符號之間的空格,然後您的變量將在條件中正確使用。

+0

我刪除了空間,但它仍不能解決問題。回聲美元currenthour完美的作品,即使與空間,所以我猜這是在條件有問題。 –

+0

第二個條件應該有一個或(||),因爲它覆蓋了兩個空格,任何大於16的數字和小於1的數字。 – theofpa

+0

哦,沒有注意到。非常感謝。問題修復 –