bash脚本 - 如果语句不按预期行为

发布于 2025-02-03 20:44:00 字数 1413 浏览 4 评论 0原文

这是我第一次玩Bash脚本,但是我只是不明白为什么如果语句不行为。无论我输入哪个输入,作为输入,我总是会返回第一个回声,只有在我输入Yes或否以外的任何其他内容时才能执行。

我尝试删除括号,用双引号封闭,但我对此无法理解。

这是代码:

#!/bin/bash

read -p 'Would you like to take driving lessons: ' choice
read -p 'Type your age: ' age

if [ $choice != 'yes' ] || [ $choice != 'no' ] 
then 
  echo 'Your choice must be either yes or no'
fi

这是输出:

$ ./test.sh        
Would you like to take driving lessons: yes
Type your age: 46
Your choice must be either yes or no
                                                                                                                                                                                                                                                                                                                           
$ ./test.sh        
Would you like to take driving lessons: no
Type your age: 63
Your choice must be either yes or no
                                                                                                                                                                                                                                                                                                                           
$ ./test.sh
Would you like to take driving lessons: dgdgf
Type your age: 76
Your choice must be either yes or no

只有最后一个运行才能返回回声语句。

This is my first time playing with bash scripting, but I just cannot understand why this if statement is not behaving. No matter what input I enter into the choice variable as input I always get returned the first echo which should only execute if I input anything other than yes or no.

I've tried removing brackets, enclosing in double quotes, but I just can't make sense of this.

Here is the code:

#!/bin/bash

read -p 'Would you like to take driving lessons: ' choice
read -p 'Type your age: ' age

if [ $choice != 'yes' ] || [ $choice != 'no' ] 
then 
  echo 'Your choice must be either yes or no'
fi

Here is the output:

$ ./test.sh        
Would you like to take driving lessons: yes
Type your age: 46
Your choice must be either yes or no
                                                                                                                                                                                                                                                                                                                           
$ ./test.sh        
Would you like to take driving lessons: no
Type your age: 63
Your choice must be either yes or no
                                                                                                                                                                                                                                                                                                                           
$ ./test.sh
Would you like to take driving lessons: dgdgf
Type your age: 76
Your choice must be either yes or no

Only the last run should return the echo statement.

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

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

发布评论

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

评论(2

污味仙女 2025-02-10 20:44:00

你很近。当它不等于时,您需要&
这起作用:

#!/bin/bash

read -p 'Would you like to take driving lessons: ' choice
read -p 'Type your age: ' age

if [ $choice != 'yes' ] && [ $choice != 'no' ];
then
  echo 'Your choice must be either yes or no'
fi

它给出以下输出:

~ bash test.sh
Would you like to take driving lessons: yes
Type your age: 432
~ bash test.sh
Would you like to take driving lessons: fdsa
Type your age: 32
Your choice must be either yes or no

You were very close. You need && when it's not equal to.
This works:

#!/bin/bash

read -p 'Would you like to take driving lessons: ' choice
read -p 'Type your age: ' age

if [ $choice != 'yes' ] && [ $choice != 'no' ];
then
  echo 'Your choice must be either yes or no'
fi

It gives the following output:

~ bash test.sh
Would you like to take driving lessons: yes
Type your age: 432
~ bash test.sh
Would you like to take driving lessons: fdsa
Type your age: 32
Your choice must be either yes or no
镜花水月 2025-02-10 20:44:00

您应该这样写:

#!/bin/bash

read -p 'Would you like to take driving lessons: ' choice
read -p 'Type your age: ' age

if [ $choice != 'yes' ] && [ $choice != 'no' ]
then
  echo 'Your choice must be either yes or no'
fi

You should write it like this:

#!/bin/bash

read -p 'Would you like to take driving lessons: ' choice
read -p 'Type your age: ' age

if [ $choice != 'yes' ] && [ $choice != 'no' ]
then
  echo 'Your choice must be either yes or no'
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文