Shell脚本中的嵌套If语句

5
这是我的脚本:
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
名称
abcd
hello.sh: 第14行:在寻找匹配“”时出现意外的EOF
hello.sh: 第16行:语法错误:意外的文件结尾

这里有什么问题吗?这可能是一个非常愚蠢的问题,但我浪费了近一个小时。

4个回答

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

您在 $password 前面有一个不匹配的 "


3

您可以使用case语句。以下是一个例子。出于安全原因,您不希望告诉任何人哪个组件有问题。

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

0

在 Shell 脚本中。

if condition
    then
        if condition
        then
            .....
            ..
            do this
        else
            ....
            ..
            do this
        fi
    else
        ...
        .....
        do this
    fi

-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

5
请格式化您的代码并添加简短的说明。 - Nikolay Mihaylov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接