bash 脚本中的嵌套案例

发布于 2024-08-05 07:57:23 字数 662 浏览 2 评论 0原文

我在 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

}

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

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法回应 2024-08-12 07:57:23

这篇文章讨论了dialog;我对此没有经验。

您的“case retval in”需要为“case $retval in”(或“case "$retval" in”)。

[@Idelic 指出我原来的答案比必要的更加保守。]

字符串“retval”与您在外壳语句中列出的选项都不匹配(使用“*”选项来检测意外的情况) )。如果 $retval 包含空格,双引号可以防止发生意外;一般来说,在语句中的 case "$var" 中(以及大多数其他地方也是如此),最好在变量周围使用双引号。在这种特殊情况下,这并不重要;退出状态始终是一个数字。在 'case "$choice" in' 语句中,我对变量周围的引号感到更舒服 - 但你可能仍然是安全的(我需要阅读有关 的更多信息对话框以确定它的作用以及它是否生成空格 - 或者甚至什么都不生成)。

This article discusses dialog; I'm not experienced with it.

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

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

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 $retval ever contained spaces; in general, it is a good idea to use double quotes around the variable in case "$var" in statements (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 dialog to be sure of what it does and whether it ever generates spaces - or nothing even).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文