鍍金池/ 問答/Linux/ shell腳本中=兩端有無空格導致輸出結(jié)果不一致

shell腳本中=兩端有無空格導致輸出結(jié)果不一致

最近學習shell腳本編程遇到這個問題:
下面的代碼8、10行,如果=的兩端沒有空格,無論輸入什么顯示都是Good morning,請問這是怎么回事呢

1 #!/bin/sh
2
3 echo "Is it morning?Please answer yes or no"
4 read timeofday
5
6 echo $timeofday
7
8 if [ "$timeofday" = "yes" ];then
9 echo "Good morning"
10 elif [ "$timeofday" = "no" ];then
11 echo "Good afternoon"
12 else
13 echo "Enter yes or no"
14 exit 1
15 fi
16
17 exit 0

回答
編輯回答
裸橙

shell 的語法解析的問題,所以它把沒有空白分隔的字符串解析為一個字符串,所以是 true。

2017年5月13日 02:19