cshell


example scripts

Basics:

begin a cshell script with: #! /bin/csh -f
Note: sometimes you must begin a cshell script with: #! /usr/bin/csh -f

to continue/extend a command onto the next line: end the line with a "\"
to comment out a section of code:
: << '--snail--'
This is commented out.
--snail--

assign a string to a variable: set atm_file=red2wr.mct
assign an integer to a variable: set signal=20
assign a float to a variable: set AT = `echo "scale=3;3/5" | bc`
"scale" sets the number of decimal places.
If you don't use "bc", all numbers must be treated like integers.
to convert a float to an integer: set t = `echo $t | awk '{printf"%.0f\n",$1}'`
The zero in "%.0f" specifies that zero digits to the right of the decimal be printed.
The "\n" prints a return carriage.
assign a list of things to a variable: set list = `ls *.zbs`
To do basic math: @ M = ($file_size / $signal)
To do more complicated math: use bc
Notice variables are defined without a '$', but are referred to with a '$'.

To echo a variable without a new trailing line: echo -n $x

to isolate the 3rd-4th letters of a file name: echo $file | cut -c 3-4
to cut off the first 7 letters of a file name: echo $file | cut -c 7-
to cut off all but the last 7: echo $file | cut -c -7

to do an iterative loop:
set n = 0
while ($n < 10)
    echo "file #"$n
    @ n = ($n + 1)
end 
to loop over files in a directory:
set list = `ls *.mct`
foreach file ($list)
   echo $file
end 
if statements:
if ($n < 5) then
   echo $file
endif 
if ($pre == 7 || $pre == 6) then 
   ...
endif 
To determine if a file exists: if(! -e $1) then
To determine if a file has zero size: if(! -z $1) then

to access command line arguments: echo $1
If the command line has no arguments, $1, $2 have values of "".
The number of command line arguments: $#argv
to overwrite a file: >!
To exit a shell script: exit

Back to Resources