Switch + stirng


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Switch + stirng
# 1  
Old 01-08-2008
Question 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
# 2  
Old 01-08-2008
You should run this script using csh, And instead of u need to use set keywod before the assignment statement
# 3  
Old 01-08-2008
I wrote:
#! /bin/csh
before the lines posted in my first post, and still give my the same error
# 4  
Old 01-08-2008
Hi.

A few modifications:
Code:
#!/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:
Code:
% ./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.
# 5  
Old 01-09-2008
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
# 6  
Old 01-09-2008
Hi.

You can try:
Code:
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:
Code:
./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:
Code:
whereis csh

On my system, this returns:
Code:
/bin/csh

Then make the first line of your script:
Code:
#!/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 https://www.unix.com/unix-dummies-que...om-forums.html ... cheers, drl

---

Standard advice: avoid csh for scripting, use Bourne shell family.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Switch to su

Hi, I've put the correct root password but why do I get this below? huamin@SOL11I:~$ su Password: su: Sorry huamin@SOL11I:~$ Many Thanks & Best Regards, HuaMin (16 Replies)
Discussion started by: HuaMin
16 Replies

2. AIX

HACMP switch over

Hi I had an active passive cluster. Node A went down and all resource groups moved to Node B. Now we brought up Node A. What is the procedure to bring everything back to Node A. Node A #lssrc -a | grep cl clcomdES clcomdES 323782 active clstrmgrES cluster... (9 Replies)
Discussion started by: samsungsamsung
9 Replies

3. UNIX for Dummies Questions & Answers

Tar with -T switch

Howdy, I'm trying to tar a bunch of files into their own individual tar archives. In other words i have files a.txt thru z.txt and i want to create a.tar thru z.tar in the same folder. I've been using -T to read in the list of files to be archived but i can't get it to work. I think my problem is... (5 Replies)
Discussion started by: fistikuffs
5 Replies

4. Shell Programming and Scripting

how to access console of a switch having rj45 on switch side to db 9 female on pc side console cable

hi, how to access console of a switch having rj45 on switch side to db 9 female on pc side console cable which needs to be connected to one console server having rj11 on its side and db 9 female on other end.i.e. on switch side,console cable has rj45 and db 9 pin female connector on other side of... (1 Reply)
Discussion started by: pankajd
1 Replies

5. Shell Programming and Scripting

need help for cp with -p switch

Guys, I need to copy files from source to destination with datetime preserved I did it with cp -p <source>/file <destinaltion>/file But when I do stat command on copied file , it seems the copied file has "change time" modified. Please guide me in understanding (2 Replies)
Discussion started by: mohan_xunil
2 Replies

6. Programming

do-while inside switch

Hi All, Could anybody please explain to me, why this piece of code compiles. void duff(register char *to, register char *from, register int count) { register int n=(count+7)/8; switch(count%8){ case 0: do{ *to++ = *from++; case 7: *to++ = *from++; case... (2 Replies)
Discussion started by: lagigliaivan
2 Replies

7. UNIX for Advanced & Expert Users

switch login

Hi, How can I switch from one login to another login in UNIX. su command is disabled in my environment. Is there any alternate way to login. (1 Reply)
Discussion started by: sharif
1 Replies

8. Programming

Switch

using switch can we match for more than one values.. eg: switcha(a) { case 1, 2, 3: printf("ddd"); break; case 4, 5, 6: printf("mmm"); break; } In this case wat i found was only for the last value, i.e 3 and 6 the switch works. ... (12 Replies)
Discussion started by: abey
12 Replies

9. Shell Programming and Scripting

can you switch

hi, i am try to run following script in c-shell, using switch command. #!/bin/csh choice=0 while do echo "system monitor" echo " 1) system paging 2) system file inf. 3) system disk inf. 9) exit " echo "select an option: \c" read choice case $choice in 1)... (3 Replies)
Discussion started by: neer45
3 Replies
Login or Register to Ask a Question