command line args

Greg Hunt hunt at dg-rtp.rtp.dg.com
Sun Jan 27 09:23:25 AEST 1991


In article <2063 at kluge.fiu.edu>, acmfiu at serss0.fiu.edu (ACMFIU) writes:
> i want to set up a shell script to go through the args passed on it and
> act accordingly. i thought this would do but apparently not. i get a
> "switch unexpected" error.
> 
> while ($1)
> 	switch ($1)
> 		case foo:
> 			blah
> 			breaksw
> 
> 		case foofoo:
> 			blah
> 			breaksw
> 	endsw
> 
> 	shift [$1]
> end
> 
> alber chin

Well, there are a couple of things going on here:

1.  You're getting the "switch unexpected" error because the Bourne
    shell (sh) is trying to execute your script, but you are using
    C shell (csh) commands which sh doesn't recognize.  By default,
    all scripts are executed by sh.  If you want to override this
    default, you need to add "#!/bin/csh" as the first line of your
    script.  That tells the system to use a different interpreter
    to execute the script, in this case, the C shell.

2.  The while command needs an expression that evaluates to true
    (non-zero) or false (zero), but you gave it a string (the first
    argument supplied to the script).

3.  To shift one script argument left, off of the list of arguments,
    just use the shift command by itself with no arguments.  The
    brackets are not legal either.

4.  The "foreach ... end" command is an easier way to loop through
    a list like you want to, instead of using while and shift.

Below is a revised csh script that I believe will do what you want:

#!/bin/csh
foreach Thing ($*)

    switch ($Thing)

        case foo:
            echo blah
            breaksw

        case foofoo:
            echo blahblah
            breaksw

        default:
            echo blahblahblah
            breaksw

    endsw

end

The "$*" means "all script arguments", so the foreach loop will be
executed once for each word, or argument, that you provide.

I hope this helps.  For more information about writing csh scripts,
you might want to read the man page on the C shell.  Try this command:

    man csh | more

Enjoy!

-- 
Greg Hunt                        Internet: hunt at dg-rtp.rtp.dg.com
DG/UX Kernel Development         UUCP:     {world}!mcnc!rti!dg-rtp!hunt
Data General Corporation
Research Triangle Park, NC, USA  These opinions are mine, not DG's.



More information about the Comp.unix.shell mailing list