These are the familiar if...then and case structures
which will execute commands if conditions are met.
==
!=
<
>
<=
>=
&&
||
!
if CommandThe if command take the general form:
if ( condition ) then
commands to do if condition returns true
else
commands to do if condition returns false
endif
For example:
set user = $1
if ( $user == 'mary' ) then
echo Hello $user, how are you\?
else
echo I don\'t think we\'ve met. How do you do\?
endif
(set user=$1 sets user to be the argument given
at runtime. \ means "switch off any special meaning of
the next character".)
if (`who | wc -l` > 1) then
echo Users still logged in. Do not shut down.
else
echo All clear to shut down.
endif
Using the if command, filenames can be tested for the following:
-d filename
filename is a directory
-e filename
filename exists
-f filename
filename is a text file
-o filename
filename
-r filename
filename is readable
-w filename
filename is writable
-x filename
filename is executable
-z filename
filename is empty
For example, the following shell script, listdir, will list
a directory, but first will test to see if the given file is a
directory.
#!/bin/csh
# csh Script listdir - lists a directory only
set file=$1
if !(-e $file) then
echo "$file does not exist."
else if (-d $file) then
ls -l $file | more
else
echo "$file is not a directory."
endif
The command listdir progs will list all the files in progs,
print progs does not exist or print progs is not a directory
depending on the status of progs.
switch CommandThe switch command takes the general form:
switch ($variable)
case pattern1
:
action
breaksw
case pattern2
:
action
breaksw
......
default :
action
endsw
For example:
#!/bin/csh
# Script to find where a user is longed in from
set user=$1
set connection=`who | awk '$1=="'$user'" {print $2}'`
switch ($connection)
case "(OXVAX)" :
echo You are connected to
linux.ox.ac.uk via VAX
echo Remember to log out
from VAX after
echo logging out from linux.ox.ac.uk
breaksw
case "(LAT_08002B1DF794)" :
echo You are connected to
linux.ox.ac.uk via a
echo Decserver. Logging
out from linux.ox.ac.uk will
disconnect you.
breaksw
default : echo Your connection is from
echo $connection
endsw