0

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

}

1 Answer 1

5

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Of course! many thanks, its amazing how you can look at a script for ages and miss the most obvious typo! cheers dude!
The double quotes are not needed for simple "case $VAR in..." statements. There's at least two cases in which you never need the quotes: simple assignment (FOO=$BAR) and the "case ... in" above.
@Idelic: interesting - I just checked both Korn and Bourne (from Solaris 10) shells, and for assignments you are correct. I didn't check case. Somewhere in the last twenty-five years (and it was in the dim distant past rather than within living memory) I got burned by some of this. Whether that was older versions of shells, or a misunderstanding, or more complicated stuff with 'eval', I don't now remember - it's sad what old age does to you. I'll reformat the answer a bit to note your comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.