bash 脚本中的嵌套案例
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这篇文章讨论了
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 incase "$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 aboutdialog
to be sure of what it does and whether it ever generates spaces - or nothing even).