Bash if string = this or that

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15806860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:06:01  来源:igfitidea点击:

Bash if string = this or that

linuxbashshellubuntuif-statement

提问by bsmoo

Trying to write a script which will read what a user has imput... I know it's basic but im stuck on the first if..

试图编写一个脚本来读取用户输入的内容......我知道这是基本的但我坚持第一个如果......

echo  "Please enter yes or no (y/n)?"
read string
if [ $string = "y" -o "n" ]
   then
      echo "User selected $string"
   else
      echo "You didn't enter y/n !!!"
fi

I would like it to be if [ $backup = "y" or "n" ]

如果 [ $backup = "y" 或 "n" ] 我希望它是

Any ideas?

有任何想法吗?

采纳答案by William

Not the question you actually asked, but... You told the user to enter "yes" or "no" but only test for y or n - sure, you gave them a hint but users are hint-resistant. So maybe a looser test is in order:

不是您实际问的问题,而是...您告诉用户输入“是”或“否”,但只测试 y 或 n - 当然,您给了他们一个提示,但用户是拒绝提示的。因此,可能需要进行更宽松的测试:

echo "Please enter yes or no (y/n)"
read string
case "$string" in
    [yY]* | [nN]*) echo "User entered $string" ;;
    *) echo "I don't understand '$string'" ;;
esac

That will recognize any variation that begins with Y or N - usually that's good enough, but you could tighten up the tests. Also, since you'll probably want to do something different with a yes or no response you can expand the case (I've also tightened the tests in this one):

这将识别以 Y 或 N 开头的任何变化 - 通常这已经足够了,但您可以加强测试。此外,由于您可能想用是或否响应做一些不同的事情,您可以扩展案例(我也加强了这个测试中的测试):

case "$string" in
    [yY] | [yY][eE][sS]) echo "Here's where you process yes" ;;
    [nN] | [nN][oO]) echo "And here you deal with no" ;;
    *) echo "I don't understand '$string'" ;;
esac

You could do this with if statements but I find case more readable when more than two alternatives are possible and the test is appropriate to case syntax.

你可以用 if 语句来做到这一点,但我发现当有两个以上的替代方案时,case 更具可读性,并且测试适用于 case 语法。

回答by Halim Qarroum

Use this syntax in bash:

在 中使用此语法bash

if [ "a string" = "another one" ] ; then

# Whatever

fi

For multiple conditional statements such as OR, use:

对于多个条件语句,例如OR,请使用:

if [ "a string" = "another one" ] || [ "$foo" = "bar" ] ; then

# Whatever

fi

bashalso supports the non-standard [[ ... ]]expression, which can process a compound comparison using a single command, rather than 2 [commands:

bash还支持非标准[[ ... ]]表达式,它可以使用单个命令而不是 2 个[命令处理复合比较:

if [[ "a string" = "another one" || $foo = "bar" ]]; then

# Whatever

fi

回答by Joao Morais

You can also try:

你也可以试试:

echo  "Please enter yes or no (y/n)?"
read string
if [[ "$string" =~ ^(y|n)$ ]]
   then
      echo "User selected $string"
   else
      echo "You didn't enter y/n !!!"
fi

回答by Chirlo

I nice solution is would be with case, which is easier to extend if you want to make your input more complex

我很好的解决方案是 with case,如果你想让你的输入更复杂,它更容易扩展

case $string in
  y|n) echo "User selected $string"
      ;;
  *) echo "You didn't enter y/n !!!"
      ;;  
esac

From there you can easily modify it to accept Uppercase or whatever:

从那里您可以轻松修改它以接受大写或其他任何内容:

case $string in
  y|Y) echo "yes, Sir!"
      ;;
  n|N) echo "No, can't do"
      ;;
  *) echo "Say what?"
      ;;  
esac

Check case statementsfor more info.

检查案例陈述以获取更多信息。