Unix C-Shell Programming Notes

Shell commands can be combined in scripts files that perform complex actions. In the following, a very short introduction to the csh (and tcsh) shell programming is given. For other types of shell, the reader can consult comprehensive textbooks dedicated to this argument reported in the reading list at the end of this appendix.

A shell script files start with the command:

#! /bin/csh –f

the #! is the autostart command for the script shell command /bin/csh (this example use the c shell but it is the same for the other type of shells). The -f option is used to make a fast startup by not reading or executing the commands in .cshrc file(this is a setup file that is executed at the start of the shell). To execute the script file, it must be made executable using chmod and hence it can be invoked by typing the script file name on the command line:

chmod +x script

./script

SHELL VARIABLES 

    set c= 299792458

The value of a shell variable can be referenced by placing a $ before the name of the variable or the curl parenthesis enclosing it. The commands

     echo $c

     echo ${c}

will output the same value of the variable C. In the case of string variables, the curl parentheses can be used to append to the string variable other strings of characters.

Example:

    set fname = prog

    set cc=1

    rm ${fname}$cc.c

This script deletes the file ‘prog1.c’ form the current directory. Special variable attributes as :r can be used to extract root names or suffixes from file names:

Example:

     set fname=prog.dat

     echo ${fname:r}

     echo ${fname:s}

it will print

     prog
     dat

Arrays of data can be defined using the list variables:

     set CONST = ( 4.148 273.15 )

and the single element assigned as

    set CONST[3] = 11.01

The number of elements in a list variable can be printed using the prefix with a #.

The command echo $#CONST will print 3.

The @ command can be used for computations. For example, if you have shell variables $X and $Y, you can set a third variable $Z to their sum by

     @Z = $X + $Y

While the commands

@ Z ++

@ Z – increment or decrement of one unit the value of Z, respectively.

SPECIAL VARIABLES

The shell reserves some of the variable names for specific purposes. One important variable is the shell variable argv. This list variable contains the arguments (parameters) that are provided to the shell script at the execution. The parameters are accessible as list elements $argv[1], $argv[2], etc. or as $1, $2, etc. The number of such arguments is $#argv.

For example, consider the following script. It can evaluate the 4 arithmetic operation between two input numbers.


#! /bin/csh -f

set val1 = $argv[1]
set val2 = $argv[3]
switch ($argv[2])
case [+]:
   echo @ val1 + val 2
   breaksw
case [-]:
   echo @ val1 – val2.
   breaksw
case [*]:
   echo @ val1 * val2.
   breaksw
case [/]:
   echo @ val1 / val2.
   breaksw
default:
  echo The operation was not recognized.
  breaksw
endsw

end

Save the script in the file named eval and execute by input two numbers separated by the operation you want to perform on them. The script uses a language construct that will be described in the next paragraph.

Language Constructs

The c-shell script language has instructions for conditional execution (if-then-else; while), iterative execution (for-loop), a switch statement (it was used in the previous paragraph), and a goto statement:

  1. Conditional execution statement (if-then-else)

The syntax of the if-then-else construct is

if ( expr ) simple-command

or

if ( expr ) then <command list-1>

else

<command list-2>

endif

The true or false value of the expression expr determines the execution of the commandlist1 and commandlist2, respectively. The portion of the construct enclosed in ’[’ and ’]’ is optional.

Example


if ($#argv  3) then
echo "Error! Usage: eval val1 operation (+|-|*|/) val2"
else
  set val1 = $argv[1]
  set val2 = $argv[3]
endif

2. The switch command

The switch command was used in the example of the previous paragraph. It is a conditional statement with multiple branches. The general form of switch is:

switch (string)

case pattern1: 
   command_list1
   breaksw
case pattern2: 
   command_list2
   breaksw
default: 
command_list

endsw

The given string str is successively matched against the case patterns that can be regular expressions. Control flow is switched to where the first match occurs.

INSTRUCTIONS FOR ITERATIVE EXECUTION 

a) while

The syntax of while loop construct is

      while ( expr ) commandlist

                  end

The command list will be executed until the expression “expr” is evaluated as false.

b) foreach

The syntax of foreach loop construct is

                  foreach var ( worddlist ) commandlist

                  end

The command list is executed once for each word in the wordlist, and each time the variable var will contain the value of that word.

Example:


#! /bin/csh -f

set f = $1

foreach d (*.tif)
  echo FOUND: ${d:r}.jpg
  set f=${d:r}.jpg
  convert $d $f
end

FURTHER READINGS

  1. Shelley Powers, Jerry Peek, Tim O’Reilly, Mike Loukides. Unix Power Tools, Third Edition. O’Reilly Media, Inc.; 3rd edition (October 1, 2002)
  2. David I. Schwartz. Introduction to UNIX (2nd Edition)
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.