Switch + stirng

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

You should run this script using csh, And instead of u need to use set keywod before the assignment statement

I wrote:
#! /bin/csh
before the lines posted in my first post, and still give my the same error

Hi.

A few modifications:

#!/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.

Thank you very much, but the script still doen't work.

If I copy and paste your code on a text file, and I try to execute it, the shell give my this error:

/usr/bin/env: csh: No such file or directory

So I tryed to change the directory:

*Using: /bin/sh
./script2: 16: Syntax error: word unexpected (expecting ")")

*Using: /bin/csh
bash: ./script2: /bin/csh: bad interpreter: No such file or directory

bash: ./script2: /bin/env/csh: bad interpreter: No such file or directory

PS: I'm on UBUNTU, and I'm a student with a professor that didn't talk about
#!/usr/bin/env csh

I tryed to study it on internet, but I don't understand what is, and How to configure it

Hi.

You can try:

csh s1

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.

Please also see Rule #6 regarding school homework in the thread Simple rules of the UNIX.COM forums: ... cheers, drl

---

Standard advice: avoid csh for scripting, use Bourne shell family.