2011-04-20 153 views
0

聲明這是我的腳本:嵌套如果shell腳本

echo "Name" 

read name 

if [ "$name" == "abcd" ]; then 

echo "Password" 

read password 

if [ "$password == "pwd" ]; then 

    echo "Hello" 

else 

    echo "Wrong password" 

fi 

else 

echo "wrong username" 

fi 

這是輸出我得到當我運行它:什麼是錯在這裏

 
sh hello.sh 

Name 

abcd 

hello.sh: line 14: unexpected EOF while looking for matching `"' 

hello.sh: line 16: syntax error: unexpected end of file 

任何想法?這可能是一個非常愚蠢的,但我浪費了將近一個小時。

回答

7
if [ "$password == "pwd" ]; then 

您有一個無與倫比的"$password

+0

哦crap..that就被忽視me..thanks很多 – hari 2011-04-20 07:24:58

2

可以使用case。這是一個例子。出於安全原因,您不會想要告訴任何人哪個組件錯誤。

echo "Name" 
read name 
echo "Password" 
read password 
case "${name}${password}" in 
"abcdpwd") 
    echo "hello";; 
    *) echo "User or password is wrong";; 
esac 
+0

非常感謝這個建議太.. – hari 2011-04-20 07:27:24

-1
if [ "$name" == "abcd" ]; 
then 
    echo "Password" 
    read password 
    if [ "$password" == "pwd" ]; 
    then 
    echo "Hello" 
    else 
    echo "Wrong password" 
    fi 
else 
    echo "wrong username" 
fi 
+4

請格式化你的代碼,並添加簡短的說明。 – 2016-08-12 07:10:12