The shell lets you define variables to perform calculations, and to pass information between commands and applications. They are either integers or strings, however it is possible to perform floating-point arithmetic by using certain applications in your script (see Section 10.3). You don’t need to declare the data type explicitly; the type is determined when you assign a value to a variable.
Variables are only defined in the current process. If you want global variables, i.e. ones that are
available to all processes, you should assign them in your .cshrc
file.
Variable names comprise up to 20 letters, digits, and underscores; and should begin with a letter or underscore.
You assign a value to a variable using the set command.
flux_100
is not a real number. Multi-word strings should be enclosed in "
quotes. The spaces around
the equals sign are necessary.
You can also remove variables with the unset command. This accepts *?[ ]
wildcards to match the
names of the shell variables.
The set command is again used but the elements are space-separated lists enclosed by parentheses. Somewhat surprisingly for a shell that mimics C, array elements start at 1, like Fortran. Here are some illustrations.
colours
is "blue"
, the second is "yellow" and so on. Multi-word elements must
be in "
quotes. So the first element of label
is "Count rate"
, and the second is "Frequency"
. The
seventh element of prime
is 17
.
To obtain the value of a variable, you prefix the variable’s name with the dollar sign.
would writeNumber of runs is 333
to standard output.
This will echo The most distant quasar observed is at redshift 5.1
.
n
is greater than ten, and if it is, executes the statement within
the if…endif structure. The value is also passed into an integer parameter of the application mem2d;
and a string parameter of application display, so if n
were forty, the resultant value would be "Result
of deconvolution after 40 iterations"
.
Arithmetic, logical and string operations are presented in Sections 10 and 11.
The values of array elements are selected by appending the element index in brackets. This
would echo to standard output the string Starting search at co-ordinates x=300,
y=256.
The following illustrates use of a text array to specify the annotations of a plot created with application linplot.
There are some shorthands for specifying subsets of an array. The best way to describe them
is through some examples. All the values assume set prime = (2 3 5 7 11 13 17 19
23)
.
Syntax | Meaning | Value |
$#prime | Number of elements of variable | 9 |
$prime[*] | All elements of variable | 2 3 5 7 11 13 17 19 23 |
$prime[$] | The last element of variable | 23 |
$prime[3-5] | The third to fifth elements of variable | 5 7 11 |
$prime[8-] | The eighth to last elements of variable | 21 23 |
Here we bring some of these ingredients together. Suppose we are experimenting trying to find the
most suitable reddish background (palnum=0
) colour for image display using the palentry command
from Kappa. The script below loops using a while...end construct: all the commands inside the
construct are repeated until the condition following the while is satisfied. That might seem a little odd
here as we appear to repeat the same commands ad infinitum because the number of colours
is fixed. That doesn’t happen because of the C-shell shift command. It discards the first
element of an array and reduces the indices of the remaining elements by one, so the original
$colours[2]
becomes $colours[1]
and so on. This means that the chosen background colour
is always $colours[1]. The shift also decrements the number of colours by one. So the
script changes the background colour and pauses for five seconds for you to consider the
aesthetics.
The shell has a number of special characters otherwise known as metacharacters. These include
wildcard characters such as *?[ ]
, single and double quotes, parentheses, and the \ line
continuation.
If you assign a value to a variable or application parameter which includes one or more of these
metacharacters, you need to switch off the special meanings. This operation is called escaping. Single
characters may be escaped with a backslash. For multiple occurrences single quotes ’ ’
will pass the
enclosed text verbatim. Double quotes " "
do the same as single quotes except that $
, \, and left quote ‘
retain their special meaning.
accept
keyword. So we have to tell the shell to treat the backslash literally
by preceding it with backslash!
In the last pair of examples an NDF section (see SUN/95’s chapter called “NDF Sections”) is
specified. As the last contains a variable value to be substituted, the $
retains its normal
special meaning but the parentheses are escaped by surrounding the section in double
quotes.
Some scripts might have parameters that cannot be defaulted, and so if their values are not given on the command line, the script needs to prompt for them.
This will prompt for the parameter and store the entered value into shell variablesize
.
The C-shell has some special forms of variable. We have seen some already: the $<
for prompting, and
how to specify array elements. The remainder largely concern command-line arguments. Here they
are tabulated.
Syntax | Meaning |
${0} | The name of the script being run |
$?name | Returns |
$n | The value of the |
$argv[n] | The value of the |
$#argv | The number of arguments passed to the script |
$* | All the arguments supplied to the script |
$$ | Process identification number (useful for making temporary files with unique names) |
$1
, $2
…$#argv
. There is
no practical limit to the number of arguments you can supply.
Let’s look at a few examples. Below is a script argex.csh
.
"
).
You can see an example using $*
to process a list of files in Section 12.2.2. There are other examples of
script arguments in Section 14.
The C-shell has some predefined variables. We’ve already met argv. The most useful other ones are listed below.
Shell variable | Meaning |
cwd | Current working directory |
home | Home directory |
path | List of directories in which to search for commands |
status | The status returned by the last command, where |
user | Username |
So if you wanted to include details of the user and directory at the head of some data-processing log you might write this in your script.
This writes a heading into filelogfile
saying who ran the script, and then appends details of the
directory in which processing occurred. Note the different metacharacters, namely >
redirects the
output to a file, and »
appends to an existing file.