Table 2.1. C and TC Shell Syntax and Constructs
The shbang line | The "shbang" line is the very first line of the script and lets the kernel know what shell will be interpreting the lines in the script. The shbang line consists of a hash mark #, an exclamation point ! (called a bang), followed by the full pathname of the shell, and any shell options. Any other lines beginning with a # are used as comments.
EXAMPLE
#!/bin/csh or #!/bin/tcsh
|
Comments | Comments are descriptive material preceded by a # sign; they are not executable statements. They are in effect until the end of a line and can be started anywhere on the line.
EXAMPLE
# This is a comment
|
Wildcards | There are some characters that are evaluated by the shell in a special way. They are called shell metacharacters or "wildcards." These characters are neither numbers nor letters. For example, the *, ?, and [ ] are used for filename expansion. The ! is the history character, the < , > , >> , <&, and | symbols are used for standard I/O redirection and pipes. To prevent these characters from being interpreted by the shell they must be quoted with a backslash or quote marks.
EXAMPLE
rm *; ls ??; cat file[1-3]; !!
echo "How are you?"
echo Oh boy\!
|
Displaying output | To print output to the screen, the echo command is used. Wildcards must be escaped with either a backslash or matching quotes.
EXAMPLE
echo "Hello to you\!"
|
Local variables | Local variables are in scope for the current shell. When a script ends or the shell exits, they are no longer available; i.e., they go out of scope. Local variables are set and assigned values.
EXAMPLE
set variable_name = value
set name = "Tom Jones"
|
Global variables | Global variables are called environment variables. They are set for the currently running shell and are available to any process spawned from that shell. They go out of scope when the script ends or the shell where they are defined exits.
EXAMPLE
setenv VARIABLE_NAME value
setenv PRINTER Shakespeare
|
Extracting values from variables | To extract the value from variables, a dollar sign is used.
EXAMPLE
echo $variable_name
echo $name
echo $PRINTER
|
Reading user input | The special variable $< reads a line of input from the user and assigns it to a variable.
EXAMPLE
echo "What is your name?"
set name = $<
|
Arguments | Arguments can be passed to a script from the command line. Two methods can be used to receive their values from within the script: positional parameters and the argv array.
EXAMPLE
% scriptname arg1 arg2 arg3 ...
Using positional parameters: |
echo $1 $2 $3 | arg1 is assigned to $1, arg2 to $2, etc. |
echo $* | all the arguments |
Using the argv array: |
echo $argv[1] $argv[2] $argv[3] | |
echo $argv[*] | all the arguments |
echo $#argv | the number of arguments |
Arrays | An array is a list of words separated by whitespace. The list is enclosed in a set of parentheses.
The built-in shift command shifts off the left-hand word in the list.
Unlike C, the individual words are accessed by index values, which start at 1 rather than 0.
EXAMPLE |
set word_list = ( word1 word2 word3 ) | |
set names = ( Tom Dick Harry Fred )
shift names
| removes Tom from the list |
echo $word_list[1]
echo $word_list[2]
echo $word_list or $word_list[*]
echo $names[1]
echo $names[2]
echo $names[3]
echo $names or echo $names[*]
| displays first element of the list
displays second element of the list
displays all elements of the list |
Command substitution | To assign the output of a UNIX/Linux command to a variable, or use the output of a command in a string, the command is enclosed in backquotes.
Example |
|
set variable_name=`command`
echo $variable_name
| |
|
set now = `date`
echo $now
echo "Today is `date`"
| The command in backquotes is executed and its output is assigned to the variable now
The output of the date command is inserted in the string |
Arithmetic | Variables that will hold the results of an arithmetic computation must be preceded by an @ symbol and a space. Only integer arithmetic is provided by this shell.
EXAMPLE
@ n = 5 + 5
echo $n
|
Operators | The C and TC shells support operators for testing strings and numbers similar to those found in the C language.
EXAMPLE |
Equality: |
== |
!= |
Relational: |
> | greater than |
>= | greater than or equal to |
< | less than |
<= | less than or equal to |
Logical: |
&& | and |
|| | or |
! | nSot |
Conditional statements | The if construct is followed by an expression enclosed in parentheses. The operators are similar to C operators. The then keyword is placed after the closing parentheses. An if must end with an endif. An alternative to if/else if is the switch statement.
EXAMPLE |
The if construct is:
if ( expression ) then
block of statements
endif
The if/else construct is:
if ( expression ) then
block of statements
else
block of statements
endif
| The if/else/else if construct is:
if ( expression ) then
block of statements
else if ( expression ) then
block of statements
else if ( expression ) then
block of statements
else
block of statements
endif
switch ( "$color" )
case blue:
echo $color is blue
breaksw
case green:
echo $color is green
breaksw
case red:
case orange:
echo $color is red or orange
breaksw
default:
echo "Not a valid color"
endsw
|
The switch construct is:
switch variable_name
case constant1:
statements
case constant2:
statements
case constant3:
statements
default:
statements
endsw
|
Loops | There are two types of loops, the while and foreach loop.
The while loop is followed by an expression enclosed in parentheses, a block of statements, and terminated with the end keyword. As long as the expression is true, the looping continues.
The foreach loop is followed by a variable name and a list of words enclosed in parentheses, a block of statements, and terminates with the end keyword. The foreach loop iterates through a list of words, processing a word and then shifting it off, then moving to the next word. When all words have been shifted from the list, it ends.
The loop control commands are break and continue.
EXAMPLE
while ( expression )
block of statements
end
foreach variable ( word list )
block of statements
end
------------------------------
foreach color (red green blue)
echo $color
end
|
File testing | The C shell has a built-in set of options for testing attributes of files, such as whether it is a directory, a plain file (not a directory), a readable file, and so forth. For other types of file tests, the UNIX test command is used. See Example 2.1 for a demonstration.
EXAMPLE |
–r | Current user can read the file |
–w | Current user can write to the file |
–x | Current user can execute the file |
–e | File exists |
–o | Current user owns the file |
–z | File is zero length |
–d | File is a directory |
–f | File is a plain file |