Benches macOS

5

Nested case statements?

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Believe it or not, I can't find a clear answer to this even after a lot of searching all over the web, and while sometimes I can make it work othertimes I can't. Is it possible to nest case statements in a shell script? Something like this:

case "$var1" in

( "a" ) dosomething; dosomethingelse;;

( "b" )

case "$var2" in

( "x" ) anothercommand;;

( "y" ) yetanother;

esac ;;

esac

5 replies

Sign in or join Beachball Help to reply.

  1. 0

    bubbalouie

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Instead of using one of the pre-csh shells, e.g., sh, bsh, ksh, etc., would it be possible for you to code in csh or tcsh (*+_absolutely fuhgeddabout bash_+*)?

    If it is possible for you to code in csh or tcsh, then the whole switch/case syntax becomes much more natural-looking and, therefore, much easier to code. An example based on your code fragment follows (the indentation I put in while composing this post unfortunately will probably get stripped out when the actual post is done):

    switch ( "$var1" )

    case "a":

    # do something

    # do something else

    breaksw # a

    case "b": # contains nested switch

    switch ( "$var2" )

    case "x":

    # another command

    breaksw # x

    case "y":

    # yet another

    breaksw # y

    endsw # var2

    breaksw # b: contains nested switch

    endsw # var1

    On the other hand, it looks as though you may be trying to do something with fall-through, from case a to case b, and from case x to case y. If that is what is going on, delete the lines `breaksw # a' and `breaksw # x'.

  2. 0

    LittleSaint

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    <pre class="command">

    case "$1" in

    a)

    echo "a"

    ;;

    b)

    echo "b"

    ;;

    c)

    case "$2" in

    1)

    echo "c1"

    ;;

    2)

    echo "c2"

    ;;

    *)

    echo "c undefined"

    ;;

    esac

    ;;

    d)

    echo "d"

    ;;

    *)

    echo "undefined"

    esac

    </pre>

  3. 0

    LittleSaint

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    The above is a bash script BTW.

  4. 0

    Theodore Lee

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Strange. Thanks. Yours works fine. The script I've written, which I don't think has anything particularly unusual in it (at most two or three simple commands after each case -- no embedded fancy constructs) doesn't work. The only difference I can see is that I used the syntax ( "a" ) -- or the equivalent -- where you've used a) -- for labelling the alternatives. Mine should work since that seems to be what's in most of the examples! I haven't had time to rewrite your example using that syntax to see if that's the problem. If it isn't, I guess I'll have to build mine up piece by piece and see what causes it to fail. If none of that works I'll have to post mine and see if sharper eyes than mine can see what's wrong.

  5. 0

    Theodore Lee

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I found my problem. It had nothing to do with the syntax. It turns out I'd drafted my script on a Windows machine ('cuz that's where I was) and when I loaded the emailed file into TextWrangler it automatically set the EOL mode to Windows(CR/LF) so it was saved with the wrong EOL for Unix, which messed up some, but not all, of the script. I also think there were some problems with having used a tab before adding comments (#....) -- I found I had to take those out, but didn't explore exactly what was going wrong.