The basic idea of scripting with the shell is that you have a set of commands, and perhaps some control statements, which make the shell do a series of commands. Basically there is no difference between a script and a program.
In particular, csh can be good for writing short scripts to run test sequences. The following quote motivates using scripts in this way [2]:
There are three ways:
csh directly and then enter script commands
interactively.
% csh myScript ...
where '...' is replaced by a sequence
of arguments. The shell places these arguments in the variable
argv and then begins to read commands from the script.
#!/usr/bin/csh ...where '...' is again the arguments to the shell. The only argument we will mention here is -f which stops the shell script from sourcing your ~/.cshrc.
You can then execute the file directly, assuming it has been granted "execute" permissons.
# This line does nothing /usr/local/bin/ls # This line runs ls, but this is a comment
set MY_EDITOR /usr/local/bin/emacs)
set DATE = 'date')
#!/usr/bin/csh echo "Your display is $DISPLAY"
$?nameexpands to `1' if name is set, or to `0' if name is not set. It is the fundamental mechanism used for checking whether particular variables have been assigned values. All other forms of reference to undefined variables cause errors.
$arrayName[n]
gives access to the nth component of the variable 'arrayName'
$#arrayName
expands to the number of elements in the variable name.
joe@cab104:~> csh % set myArray = (a b c) % echo $?myArray 1 % echo $#myArray 3 % echo $myArray[2] b % unset myArray % echo $?myArray 0 % echo $myArray Undefined variable: myArray. %
#!/usr/bin/csh
echo "You entered $#argv arguments"
echo "1st one= $1" # method 1
echo "2nd one= $argv[2]" # method 2
And the output looks:
justin@cab104:~>./myScript hi there
You entered 2 arguments
1st one= hi
2nd one= there
if (expr) then
commands
else if (expr) then
commands
else
commands
endif
while (expr)
commands
end
foreach vrbl (list)
commands
end
switch (string)
case pattern:
commands
breaksw
case pattern:
.
.
default:
commands
endsw
repeat count command
#!/usr/bin/csh
repeat 2 echo Greetings!
echo
foreach i (1 3 five)
echo "i = [$i]"
end
echo
set BEST_EDITOR = "emacs"
switch ($EDITOR)
case $BEST_EDITOR:
echo "You are cool"
breaksw
default :
echo "You are not as cool as an emacs user"
breaksw
endsw
And the output looks like:
justin@cab104:~>./myScript
Greetings!
Greetings!
i = [1]
i = [3]
i = [five]
You are cool
-? filename
where `?' is replaced by a number of single characters:
| -r file | -- test if file can be read |
| -w file | -- test if file can be written to |
| -x file | -- test if file can be executed |
| -d file | -- test if file is a directory |
| -e file | -- test if file exists |
| -o file | -- test if you are the owner of file |
| -z file | -- test if file is empty |
| -f file | -- test if file is an ordinary file, that is, not a direcotry, not a character special, and not a block special file |
/usr/local/bin/ls in place of just plain ls
#!/usr/bin/csh set LS = /usr/local/bin/ls $LS