2011-04-20 130 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