bash 一个案例中有多个命令?

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

Multiple commands in a case?

bash

提问by dominique120

Is it possible to do this:

是否有可能做到这一点:

 case $ans1_1 in
     y)fedoraDeps;;
      echo "something here";;
      make -j 32;;
     n)echo "Continuing"...;;
      ;;
     *) echo "Answer 'y' or 'n'";;
  esac

Where fedoraDepsis a function with yumcommands.

fedoraDepsyum命令的函数在哪里。

I'm trying to replicate this with cases:

我试图用案例复制这一点:

if [[ $ans1_1 = y ]]; then
    fedoraDeps
    echo "something here"
    make -j 32
elif [[ $ans1_1 = n ]]; then
      echo "Continuing..."
      :
else
    echo "Answer 'y' or 'n'"
fi

回答by that other guy

;or line feed is used to end a command. ;;is used to end the case branch. Just don't try to end the case branch after every command, and it's fine:

;或换行用于结束命令。;;用于结束 case 分支。只是不要尝试在每个命令之后结束 case 分支,这很好:

case $ans1_1 in
    y)
      fedoraDeps
      echo "something here"
      make -j 32 ;;
    n)
      echo "Continuing"... ;;
    *) 
      echo "Answer 'y' or 'n'" ;;
esac

回答by Dale_Reagan

Due to variations in shell behaviors I suggest using spaces before semicolons... Single ';' allow for what you want. Double ';' 'end' the case 'match', i.e.

由于 shell 行为的变化,我建议在分号前使用空格......单个 ';' 允许你想要什么。双倍的 ';' “结束”案例“匹配”,即

This should work:

这应该有效:

case $ans1_1 
in
     y) fedoraDeps ;
        echo "something here" ;
        make -j 32 ;; ## last command for case 'match'
     n) echo "Continuing"... ;;
        ## if you want a blank line then just use one
     *) echo "Answer 'y' or 'n'" ;;
esac