What is a Shell Script?


Expressions


Control Structures

Script languages make use of programming control structures, such as "if" statements and "loops". Those for C Shell are described below.

if


if / then / else


foreach / end


while / end


break

Used to interrupt the execution of a foreach or while loop. Transfers control to the statement after the end statement, thus terminating the loop. If there are other commands on the same line as a break statement, they will be executed before the break occurs. Multi-level breaks are thus possible by writing them all on one line.


continue

Used to interrupt the execution of a foreach or while loop. Transfers control to the end statement, thus continuing the loop. If there are other commands on the same line as a continue statement, they will be executed before the continue occurs.


goto

The goto statement transfers control to the statement beginning with label:


switch / case / breaksw / endsw


Interrupt handling

The onintr statement transfers control when you interrupt (CTRL-C) the shell script. Control is transferred to the statement beginning with label:

Can be useful for gracefully cleaning up temporary files and exiting a program should it be interrupted.


Miscellaneous

Miscellaneous tasks for C Shell programming are described below.

Using quotes


Storing the output of a command

The shell uses backquotes to obtain the output of the command enclosed within the backquotes. This output can be stored within an array variable. Each element can then be indexed and processed as required.


Reading user input

Depending on your system, you can use either "$<" or the output of the "head -1" command to read stdin into a variable. Note that if you use the "head -1" command, it must be enclosed in backquotes.

Note: Be careful on making sure that you use "$<" and not "<$". The latter case will usually cause your script to fail.


This concludes the tutorial. Return to the Table of Contents