while 循环的多个条件

Cod*_*ler 3 shell-script

我有一个名为choice. 我想提示输入一个值,choice直到它不为空并且等于yesor no

换句话说:虽然choice是空的或不同的是yes,并no随后进入选择。

如果我用 Java 编写它,它将是小菜一碟:

while (choice.IsEmpty() || (!choice.equals("yes") && !choice.equals("no"))
Run Code Online (Sandbox Code Playgroud)

但是我找不到shell不使用内容(双引号之间的 var)语法将其转换为的方法:

while [ "$choice" != "yes" && "$choice" != "no" ]
Run Code Online (Sandbox Code Playgroud)

显然它有效,但是,就我个人的知识而言,是否有另一种方法可以像在 Java 中一样进行测试?

ilk*_*chu 21

这个

while [ "$choice" != "yes" && "$choice" != "no" ]
Run Code Online (Sandbox Code Playgroud)

实际上不起作用,因为&&中断了[命令。使用以下任一项:

while [  "$choice" != "yes" ] && [ "$choice" != "no" ]
while [[ "$choice" != "yes"   &&   "$choice" != "no" ]]
Run Code Online (Sandbox Code Playgroud)

在 Bash 和其他功能丰富的 shell 中,您还可以使用正则表达式匹配:

while [[ ! $choice =~ ^(yes|no)$ ]]
Run Code Online (Sandbox Code Playgroud)

或者 ksh 风格的扩展 glob,它可能需要至少在 Bash 和 Zsh 中显式启用:

# shopt -s extglob        # Bash, though not needed within [[ .. ]] 
                          #       in recent versions
# setopt kshglob          # Zsh
while [[ $choice != @(yes|no) ]]
Run Code Online (Sandbox Code Playgroud)

或者在 Zsh 中,使用 Zsh 自己的扩展 glob 语法:

while [[ $choice != (yes|no) ]]
Run Code Online (Sandbox Code Playgroud)

(例如 ,有关不同扩展球体之间的关系,请参阅此答案。)

  • 请注意,由于 4.1 extglob 在 bash 中的 `[[...]]` 中自动启用。 (2认同)

Sté*_*las 13

While (choice.IsEmpty() || (!choice.equals("yes") && !choice.equals("no"))POSIXsh语法中的字面翻译如下所示:

while
  [ -z "$choice" ] || {
    ! [ "$choice" = yes ] &&
    ! [ "$choice" = no ]
  }
do
 ...
done
Run Code Online (Sandbox Code Playgroud)

对于更接近的匹配,您可以使用ksh93which 具有对对象编程的实验支持:

typeset -T java_string=(
  function IsEmpty
  {
    [[ -z $_ ]]
  }
  function equals
  {
    [[ $_ = "$1" ]]
  }
)
Run Code Online (Sandbox Code Playgroud)

java_string用两个IsEmptyandequals方法声明一个对象类型,然后:

java_string choice
while
  IFS= read -r 'choice?Enter your choice: ' || exit
  choice.IsEmpty || {
    ! choice.equals yes &&
    ! choice.equals no
  }
do
  print -ru2 "Wrong choice: $choice, try again."
done
Run Code Online (Sandbox Code Playgroud)

但 the[ -z "$choice" ]是多余的,因为 if$choiceyesor no,显然它不是空的。

until
  [ "$choice" = yes ] || [ "$choice" = no ]
do
  ...
done
Run Code Online (Sandbox Code Playgroud)

会更有意义。

使用 Korn shell(或zsh -o kshgloborbash -O extglobbash4.1+),您还可以执行以下操作:

until
  [[ $choice = @(yes|no) ]]
do
  ...
done
Run Code Online (Sandbox Code Playgroud)


row*_*oat 12

有没有另一种方法来测试

您可以使用标准case构造:

case $choice in yes|no) false ;; esac
Run Code Online (Sandbox Code Playgroud)

即使在 a 的条件部分中也可以使用它while,尽管构造最终可能会有点混乱:

while case $choice in yes|no) false ;; esac; do 
Run Code Online (Sandbox Code Playgroud)

和Java一样吗?