bash 案例陈述失败?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12010686/
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-09 22:33:20  来源:igfitidea点击:

Case statement fallthrough?

bash

提问by Resorath

In popular imperative languages, switch statements generally "fall through" to the next level once a case statement has been matched.

在流行的命令式语言中,一旦匹配了 case 语句,switch 语句通常会“落入”下一个级别。

Example:

例子:

int a = 2;
switch(a)
{
   case 1:
      print "quick ";
   case 2: 
      print "brown ";
   case 3: 
      print "fox ";
      break;
   case 4:
      print "jumped ";
}

would print "brown fox".

将打印“棕色狐狸”。

However the same code in bash

但是在 bash 中相同的代码

A=2
case $A in
2)
  echo "QUICK"
  ;&
2)
  echo "BROWN"
  ;&
3)
  echo "FOX"
  ;&
4)
  echo "JUMPED"
  ;&
esac

only prints "BROWN"

只打印“棕色”

How do I make the case statement in bash "fall through" to the remaining conditions like the first example?

如何使 bash 中的 case 语句像第一个示例一样“落入”其余条件?

(edit: Bash version 3.2.25, the ;& statement (from wiki) results in a syntax error)

(编辑:Bash 版本 3.2.25, ;& 语句(来自 wiki)导致语法错误)

running:

跑步:

test.sh:

测试.sh:

#!/bin/bash
A=2
case $A in
1)
  echo "QUICK"
  ;&
2)
  echo "BROWN"
  ;&
3)
  echo "FOX"
  ;&
esac

Gives:

给出:

./test.sh: line 6: syntax error near unexpected token ;' ./test.sh:
line 6:
;&'

./test.sh:第 6 行:意外标记附近的语法错误 ;&';' ./test.sh:
line 6:

采纳答案by geirha

The ;&and ;;&operators were introduced in bash 4.0, so if you want to stick with a five year old version of bash, you'll either have to repeat code, or use ifs.

;&;;&运营商在bash 4.0中引入的,所以如果你想坚持一个五岁版本的bash,你要么必须重复代码,或使用if秒。

if (( a == 1)); then echo quick; fi
if (( a > 0 && a <= 2)); then echo brown; fi 
if (( a > 0 && a <= 3)); then echo fox; fi
if (( a == 4)); then echo jumped; fi

or find some other way to achieve the actual goal.

或者寻找其他方式来实现实际目标。

(On a side note, don't use all uppercase variable names. You risk overwriting special shell variables or environment variables.)

(附带说明,不要使用所有大写的变量名称。您可能会覆盖特殊的 shell 变量或环境变量。)

回答by René Steetskamp

Try this:

尝试这个:

case $VAR in
normal)
    echo "This doesn't do fallthrough"
    ;;
fallthrough)
    echo -n "This does "
    ;&
somethingelse)
    echo "fall-through"
    ;;
esac

回答by Mecki

Using ;&is not very portable, as it requires bash(not ash, dash, or any other minimal sh) and it requires at least bash4.0 or newer (not available on all systems, e.g. macOS 10.14.6 still only offers bash 3.2.57).

使用;&不是很便携,因为它需要bash(不是ashdash或任何其他最小的sh)并且它至少需要bash4.0 或更高版本(并非在所有系统上都可用,例如 macOS 10.14.6 仍然只提供 bash 3.2.57)。

A work around that I consider much nicer to read than a lot of if's is loop and modify the case var:

我认为比很多if's更易于阅读的解决方法是循环并修改 case var:

#!/bin/sh

A=2
A_BAK=$A
while [ -n "$A" ]; do
    case $A in
        1)
            echo "QUICK"
            A=2
            ;;

        2)
            echo "BROWN"
            A=3
            ;;

        3)
            echo "FOX"
            A=4
            ;;

        4)
            echo "JUMPED"
            A=""
            ;;
    esac
done
A=$A_BAK