2016-09-22 85 views
-1

我是tryong來驗證一個簡單的文本輸入。 我需要驗證輸入是「是」或「否」。 我使用下面的代碼:試圖用bash驗證文本輸入

#!/bin/bash 
IsSSL=$1 
if [$IsSSL = "Yes"] || [$IsSSL = "No"]; 
then 
     echo "correct input" 
else 
     echo "incorrect input" 
fi 

,但我不斷收到以下錯誤:

#sh test.sh df 
test.sh: line 3: [df: command not found 
test.sh: line 3: [df: command not found 
incorrect input 

任何想法,我究竟做錯了什麼?

回答

0

試試這個;

#!/bin/bash 
IsSSL=$1 
if [ $IsSSL = "Yes" ] || [ $IsSSL = "No" ]; 
then 
     echo "correct input" 
else 
     echo "incorrect input" 
fi 

在bash,你需要,如果條件

0
#!/bin/bash 
IsSSL=$1 
if [ $IsSSL = "Yes" ] || [ $IsSSL = "No" ] ; 
then 
     echo "correct input" 
else 
     echo "incorrect input" 
fi 

您需要[周圍空間周圍[和]空間。

使用shellcheck這樣的東西。

此外,你應該總是引用你的變量,以避免其中的特殊字符的複雜性。所以你的條件應該是 if [ "$IsSSL" = "Yes" ] || [ "$IsSSL" = "No" ];