Shell 脚本中的嵌套 If 语句

发布于 2024-11-02 07:13:57 字数 483 浏览 2 评论 0原文

这是我的脚本:

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

知道这里出了什么问题吗?这可能是一个非常愚蠢的问题,但我在上面浪费了将近一个小时。

This is my script:

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

And this is the output I get when I run it:

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

Any idea whats wrong here? This could be a very silly one, but i wasted almost an hour on it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

金兰素衣 2024-11-09 07:13:57
if [ "$password == "pwd" ]; then

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

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

You have an unmatched " before $password

原野 2024-11-09 07:13:57

您可以使用case。这是一个例子。出于安全原因,您不会想告诉任何人哪个组件出了问题。

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

you can use case. Here's an example. You won't want to tell anybody which component is wrong, for security reason.

echo "Name"
read name
echo "Password"
read password
case "${name}${password}" in
 "abcdpwd" ) 
     echo "hello";;
  *) echo "User or password is wrong";;
esac
空城之時有危險 2024-11-09 07:13:57

在 Shell 脚本中。

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

In Shell script.

if condition
    then
        if condition
        then
            .....
            ..
            do this
        else
            ....
            ..
            do this
        fi
    else
        ...
        .....
        do this
    fi
捂风挽笑 2024-11-09 07:13:57
if [ "$name" == "abcd" ];
then
  echo "Password"
  read password
  if [ "$password" == "pwd" ];
  then
    echo "Hello"
  else
    echo "Wrong password"
  fi
else
  echo "wrong username"
fi
if [ "$name" == "abcd" ];
then
  echo "Password"
  read password
  if [ "$password" == "pwd" ];
  then
    echo "Hello"
  else
    echo "Wrong password"
  fi
else
  echo "wrong username"
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文