Previous Table of Contents Next


MISCELLANEOUS

# anything following a # to the end of the current line is treated as a comment and ignored. If the first character of a script file is #, a C shell is invoked. Otherwise a Bourne shell is invoked.
#!interpreter if the first line of a script file starts with this, then the script is run by the specified interpreter. For example, #!/bin/ksh would cause a Korn shell to be invoked. C shell options flags can also be enabled here.

DEBUGGING C SHELL SCRIPTS

The C shell provides a number of options that are useful in debugging scripts: –n, –v, and –x (see C Shell Options). The –n option causes commands to be read without being executed and is used to check for syntax errors. The –v option causes the input to displayed as it is read. The –x option causes commands in C shell scripts to be displayed as they are executed. This is the most useful, general debugging option. For example, tscript could be run in trace mode if invoked “csh –x tscript”.

FILES

~/.cshrc read at beginning of execution by C shell
~/.history contains previously executed commands
~/.login read by login shell after .cshrc file
~/.logout read by login shell at logout

REGULAR EXPRESSIONS

Regular expressions are used in many UNIX commands, including awk, ed, egrep, grep, sed, and vi.

c non-special character c
\c special character c
^ beginning of line
$ end of line
. any single character
[abc] any character a, b, or c
[ac] any character in range a through c
[^abc] any character except a, b, or c
[^ac] any character except characters in range a through c
\n nth \(...\) match (grep only)
rexp* zero or more occurrences of regular expression rexp
rexp+ one or more occurrences of regular expression rexp
rexp? zero or one occurrence of regular expression rexp
rexp1 | rexp2 regular expressions rexp1 or rexp2
\(rexp\) tagged regular expression rexp (grep only)
(rexp) regular expression rexp (egrep only)

MISC UNIX COMMANDS

The following commands are frequently used in C shell scripts to filter input and output.

awk/nawk - Pattern Scanning and Processing Language

$awk [ options ] [ 'program' ] [ parameters ] [ files ]
$nawk [ options ] [ 'program' ] [ files ]

Description:

The awk/nawk command performs actions for lines in files that match patterns specified in program. Each input line is made up of fields separated by whitespace.

Options:

–ffile get patterns from file instead of program
–Fc separate fields with character c (default whitespace)
–v variable=value assign value to variable (nawk only)
parameters parameters have the format variable=expression
files read standard input if files is or no files are specified

Program Format:

Patterns in program can be associated with a statement to perform if an input line matches the pattern. The format is:

{ statement }

A missing pattern always matches, and a missing statement prints the current input line.

Patterns:

BEGIN match before first input line
END match after last input line
pattern1, pattern2, ..., patternn match if pattern1, pattern2, or patternn match current input line
pattern1 && pattern2 match if pattern1 and pattern2 match current input line
pattern1 || pattern2 match if pattern1 or pattern2 match current input line
!pattern match if pattern does not match current input line
/regular-expression/ match if regular-expression matches current input line
relational-expression match if relational-expression evaluates to true

Flow Control Statements:

break exit from for or while loop
continue execute next for or while loop
delete variable[expression] delete element expression from array variable
do statement while (expression) execute statement while expression is true
exit skip remaining input
for (expression1; expression2; expression3) statement execute statement while expression2 is true; loop is usually initialized with expression1 and incremented with expression3
for (variable in array) statement execute statement, setting variable to successive elements in array
if (expression) statement1 [ else statement2 ] execute statement1 if expression is true, otherwise execute statement2
next skip rest of the input line
return[expression] return value of expression
system(command) execute command and return status
while (expression) statement execute statement while expression is true

Input/Output Statements:

close(file) close file
getline set $0 to next input record (set NF, NR, FNR)
getline<file set $0 to next input from file (set NF)
getline variable set variable to next input record (set NR, FNR)
getline variable<file set variable to next input record from file
command | getline pipe output of command into getline
print print current input record
print expression print expression; multiple expressions must be separated with a “,”
print expression>file print expression to file; multiple expressions must be separated with a “,”
printf format expression print expression according to C-like format. Multiple expressions must be separated with a “,”. Output can also be appended to file using >>, or piped to a command using |.
printf format expression>file print expression to file according to C-like format. Multiple expressions must be separated with a “,”. Output can also be appended to file using >>, or piped to a command using |.


Previous Table of Contents Next
1