Nested case statements?

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

Mini, G4 desktop, and powerbook G4, Mac OS X (10.4)

Posted on Apr 23, 2008 9:38 AM

Reply
5 replies

Apr 23, 2008 11:43 AM in response to Theodore Lee

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

Apr 24, 2008 7:01 AM in response to LittleSaint

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.

Apr 25, 2008 7:28 AM in response to Theodore Lee

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.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Nested case statements?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.