Hi,
This script receive in input 2 parameters, the use $2 in this way:
switch ($2)
case r:
p=r--
echo $2 ok
breaksw
case rw:
p=rw-
echo $2 ok
breaksw
case rwx:
p=rwx
echo $2 ok
breaksw
default
echo Errore, secondo parametro errato #exit 0
endsw
Why I have the error???:
./script: line 1: syntax error near unexpected token `$2'
./script: line 1: `switch ($2)'
I followed this guide:
switch ( str )
case string1:
commandlist1
breaksw
case string2:
commandlist2
breaksw
default
commandlist
endsw
#!/usr/bin/env csh
# @(#) s1 Demonstrate switch command.
if ( $#argv < 2 ) then
echo " Need 2 arguments."
exit 1
endif
echo
echo "(Versions displayed with local utility version)"
sh -c "version >/dev/null 2>&1" && version tcsh
echo
echo " Before end of switch."
switch ($2)
case r:
set p=r--
echo $2 ok
breaksw
case rw:
set p=rw-
echo $2 ok
breaksw
case rwx:
set p=rwx
echo $2 ok
breaksw
default
echo Errore, secondo parametro errato
#exit 0
endsw
echo " After switch."
exit 0
producing:
% ./s1 any-string rw
(Versions displayed with local utility version)
tcsh 6.13.00
Before end of switch.
rw ok
After switch.
Best wishes ... cheers, drl
-----
Standard advice: avoid csh family for scripting, use Bourne shell family.
which works on most systems, provided that csh is installed and is in your path.
However, in order to execute the file as a command like:
./s1
you'll need the first line to contain the absolute path to csh, and to set the execute permission on the file. For the first part, enter at the command line:
whereis csh
On my system, this returns:
/bin/csh
Then make the first line of your script:
#!/bin/csh
or whatever the whereis command returns. (The first line in a script is very special. It looks like a comment, but it actually supplies the direct path to the program that is to process the script. The first line is called the shebang line.)
If whereis cannot find csh, then either you'll need to install it -- if csh is a requirement for the class -- or you'll need to learn to write the script in bash (or some other Bourne shell family shell).
If all else fails, perhaps you need to chat with the prof.