3

In sh, I know I can do something like the following...

#!/sbin/sh

case "$1" in
'foo')
/path/to/foo.sh
;;
'bar')
/path/to/bar.sh
;;
*)
echo $"Usage: $0 {foo|bar}"
exit 1
;;
esac

I've searched the entirety of the internets and can't seem to find a corresponding switch statement that matches what I'm trying to do above.

Before we get too much further; I know, I know, I shouldn't be using csh, but I have a legacy app that only runs in csh. I'm trying to build a Solaris SMF service for it and I've already tried setting the shell but whatever shell is used in the method file overwrites the actual SMF shell. So it has to be in csh.

Thanks in advance for any help provided.

tl;dr (How do you convert the above to csh?)

  • man csh gives some reference material regarding case and switch. – Chris Davies Jul 05 '19 at 15:00
  • So would that be something like: switch $1 case foo: /path/to/foo.sh breaksw case bar: /path/to/bar.sh breaksw default: breaksw endsw

    ...sorry I can't seem to format stuff in a comment.

    – Trae McCombs Jul 05 '19 at 15:24
  • 1
    Ya gotta love Solaris and their never-ending backwards compatibility layers that let old legacy apps that should have died in the last millenium keep running. – Tim Kennedy Jul 05 '19 at 15:25

1 Answers1

3

I'm glad to see you're aware of the downfalls of C-Shell.

Here is the same script, converted to C-shell (Edit: updated to output $usage in cases of no args, or more than 1 arg):

#!/usr/bin/env csh

set usage="Usage: $0 {foo|bar}"

if ( $#argv != 1 ) then
  echo $usage
else
  switch ($argv[1])
  case 'foo':
    /path/to/foo.sh
    breaksw
  case 'bar':
    /path/to/bar.sh
    breaksw
  default:
    echo $usage
    breaksw
  endsw
endif
Tim Kennedy
  • 19,697