bash 脚本中的嵌套大小写

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

Nested case in bash script

bashselectscriptingcase

提问by Morchuboo

I have written the following function in a bash script but it is not working. Am I missing something obvious?

我在 bash 脚本中编写了以下函数,但它不起作用。我错过了一些明显的东西吗?

main_menu() {
dialog \
    --title "Sim Gateway Infomation Utility" \
    --menu "What do you want to do?" 12 60 5 \
    Summary "View overall summary" \
    Details "View details of a sim bank" \
    Modify "Modify used minutes of a sim" \
    Exit "Exit" \
    2>$tempfile

retval=$?
case retval in
    0)

        choice=`cat $tempfile`
        case $choice in
            Summary) summary;;
            Details) details;;
            Modify) modify;;
            Exit) clean_up;;
        esac
        ;;
    1)
        confirm_exit;;
    255)
        confirm_exit;;
esac

}

}

回答by Jonathan Leffler

This articlediscusses dialog; I'm not experienced with it.

文章讨论dialog; 我没有这方面的经验。

Your 'case retval in' needs to be 'case $retval in(or 'case "$retval" in).

您的 ' case retval in' 必须是 ' case $retval in(或 ' case "$retval" in)。

[@Idelic notes that my original answer was more conservative than necesssary.]

[ @Idelic 指出,我最初的回答比必要的更保守。]

The string 'retval' matches none of the options you list in your outer case statement (use a '*' option to detect the unexpected). The double quotes prevent accidents if $retvalever contained spaces; in general, it is a good idea to use double quotes around the variable in case "$var" instatements (and most other places too). In this particular case, it would not matter; the exit status is always a number. In the 'case "$choice" in' statement, I'd be more comfortable with the quotes around the variable - but you may still be safe (I'd need to read more about dialogto be sure of what it does and whether it ever generates spaces - or nothing even).

字符串 'retval' 不匹配您在外部 case 语句中列出的任何选项(使用 '*' 选项来检测意外情况)。如果$retval包含空格,双引号可以防止发生意外;一般来说,最好在case "$var" in语句中的变量周围使用双引号(以及大多数其他地方)。在这种特殊情况下,这无关紧要;退出状态始终是一个数字。在 ' case "$choice" in' 语句中,我会更喜欢变量周围的引号 - 但你可能仍然是安全的(我需要阅读更多关于dialog它的功能以及它是否会产生空格 - 或者什么都不产生甚至)。