bash脚本中的嵌套大小写 [英] Nested case in bash script
问题描述
我已经在bash脚本中编写了以下函数,但是它不起作用. 我缺少明显的东西吗?
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
}
推荐答案
此文章讨论了dialog
;我没有经验.
This article discusses dialog
; I'm not experienced with it.
您的"case retval in
"必须为"case $retval in
"(或"case "$retval" in
").
Your 'case retval in
' needs to be 'case $retval in
(or 'case "$retval" in
).
[ @Idelic指出,我的原始答案比必要的更为保守.]
字符串"retval"与您在大写语句中列出的所有选项都不匹配(使用"*"选项可检测到意外内容).如果$retval
包含空格,则双引号可防止发生意外.通常,在case "$var" in
语句(以及大多数其他地方)中的变量周围使用双引号是个好主意.在这种特殊情况下,这无关紧要;退出状态始终是数字.在'case "$choice" in
'语句中,我对变量的引号会比较满意-但您可能仍然很安全(我需要阅读有关dialog
的更多信息,以确保其作用以及是否对其起作用.会产生空间-甚至不会产生任何空间.
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).
这篇关于bash脚本中的嵌套大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!