The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
switch login sharif UNIX for Advanced & Expert Users 1 12-12-2007 01:03 AM
Switch abey High Level Programming 12 06-27-2006 04:11 PM
switch from csh to ksh veeracer Shell Programming and Scripting 8 11-05-2004 11:13 AM
switch from csh to ksh veeracer UNIX for Dummies Questions & Answers 1 11-03-2004 03:28 PM
can you switch neer45 Shell Programming and Scripting 3 12-05-2001 05:54 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-08-2008
Registered User
 

Join Date: Jan 2008
Posts: 69
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
Reply With Quote
Forum Sponsor
  #2  
Old 01-08-2008
Registered User
 

Join Date: Nov 2007
Posts: 85
You should run this script using csh, And instead of u need to use set keywod before the assignment statement
Reply With Quote
  #3  
Old 01-08-2008
Registered User
 

Join Date: Jan 2008
Posts: 69
I wrote:
#! /bin/csh
before the lines posted in my first post, and still give my the same error
Reply With Quote
  #4  
Old 01-08-2008
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 556
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.
Reply With Quote
  #5  
Old 01-09-2008
Registered User
 

Join Date: Jan 2008
Posts: 69
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
Reply With Quote
  #6  
Old 01-09-2008
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 556
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 Simple rules of the UNIX.COM forums: ... cheers, drl

---

Standard advice: avoid csh for scripting, use Bourne shell family.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux, ubuntu

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0