#!/bin/csh
chmod u+x myscript
#Simple Script # echo 'Welcome to the world of script files' date
Example 1: Mathematical expression, where "a" and "t" are variables, "3" and "4" are constants, and "=" and "+" are operators.
a = 3 + 4t
Example 2: Logical expression, which evaluates to either "true" or "false". The string "exit" is a constant, "var" is a variable, and "(", ")" and "==" are operators.
($var == exit)
() - parenthesis - change order of evaluation - - unary minus/negation ~ - one's complement ! - logical negation % - remainder / - divide * - multiply - - subtract + - addition >> - shift right << - shift left > - greater than < - less than >= - greater than or equal <= - less than or equal != - not equal to (strings) == - equal to (strings) & - bitwise AND ^ - bitwise exclusive OR | - bitwise inclusive OR && - logical AND || - logical OR
Used to test an expression and then conditionally execute a command. If the specified expression evaluates true, then the single command with arguments is executed. Command must be a simple command, not a pipeline, a command list, or a parenthesized command list.
Syntax:
if (expr) command [arguments]Example:
#!/bin/csh if ($#argv == 0) echo There are no arguments
In addition to the C Shell's logical expressions, you can use expressions that return a value based upon the status of a file. For example:
if (-e myfile) echo myfile already existsThe possible file status expressions are:
d - file is a directory e - file exists f - file is an ordinary file o - user owns the file r - user has read access to the file w - user has write access to the file x - user has execute access to the file z - file is zero bytes long
Used to test multiple conditions and to execute more than a single command per condition. If the specified expr is true then the commands to the first else are executed; otherwise if expr2 is true then the commands to the second else are executed, etc. Any number of else-if pairs are possible; only one endif is needed. The else part is likewise optional.
The words else and endif must appear at the beginning of command lines; the if must appear alone on its command line or immediately after an else.
Syntax:
if (expr) then commands else if (expr2) then commands else commands endifExample:
#!/bin/csh if ($#argv == 0) then echo "No number to classify" else if ($#argv > 0) then set number = $argv[1] if ($number < 0) then @ class = 0 else if (0 <= $number && $number < 100) then @ class = 1 else if (100 <= $number && $number < 200) then @ class = 2 else @ class = 3 endif echo The number $number is in class $class endif
The foreach statement is a type of loop statement. The variable name is successively set to each member of wordlist and the sequence of commands until the matching end statement are executed. Both foreach and end must appear alone on separate lines.
Syntax:
foreach name (wordlist) commands endExample:
#!/bin/csh foreach color (red orange yellow green blue) echo $color end
The while statement is another type of loop statement. Statements within the while/end loop are conditionally executed based upon the evaluation of the expression. Both while and end must appear alone on separate lines.
Syntax:
while (expression) commands endExample:
#!/bin/csh set word = "anything" while ($word != "") echo -n "Enter a word to check (Return to exit): " set word = $< if ($word != "") grep $word /usr/share/dict/words end
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.
breakExample:
#!/bin/csh foreach number (one two three exit four) if ($number == exit) then echo reached an exit break endif echo $number end
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.
continueExample:
#!/bin/csh foreach number (one two three exit four) if ($number == exit) then echo reached an exit continue endif echo $number end
The goto statement transfers control to the statement beginning with label:
goto labelExample:
#!/bin/csh if ($#argv != 1) goto error1 if ($argv[1] < 6) goto error2 goto OK error1: echo "Invalid - wrong number or no arguments" echo "Quitting" exit 1 error2: echo "Invalid argument - must be greater than 5" echo "Quitting" exit 1 OK: echo "Argument = $argv[1]" exit 1
The switch structure permits you to set up a series of tests and conditionally executed commands based upon the value of a string. If none of the labels match before a `default' label is found, then the execution begins after the default label.
Each case label and the default label must appear at the beginning of a line. The command breaksw causes execution to continue after the endsw. Otherwise control may fall through case labels and default labels. If no label matches and there is no default, execution continues after the endsw.
Syntax:
switch (string) case str1: commands breaksw case str2: commands breaksw ... default: commands breaksw endswExample:
#!/bin/csh if ($#argv == 0 ) then echo "No arguments supplied...exiting" exit 1 else switch ($argv[1]) case [yY][eE][sS]: echo Argument one is yes. breaksw case [nN][oO]: echo Argument one is no. breaksw default: echo Argument one is neither yes nor no. breaksw endsw endif
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.
onintr labelExample:
#!/bin/csh onintr close while (1 == 1) echo Program is running sleep 2 end close: echo End of program
Single quotes:
Double quotes:
Example 1: Variable substitution
#!/bin/csh set opt=-l set x1='ls $opt' echo $x1 set x2="ls $opt" echo $x2 Will produce the output: ls $opt ls -lExample 2: Filename generation
#!/bin/csh set ls1='some files: [a-z]*' echo $ls1 set ls2="some files: [a-z]*" echo $ls2 Sample output (identical): some files: csh.html images man misc other.materials some files: csh.html images man misc other.materials
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.
set variable = `command`Example:
#!/bin/csh set date_fields=`date` echo $date_fields echo $date_fields[1] echo $date_fields[2] foreach field(`date`) echo $field end Sample output: Thu Mar 9 22:25:45 HST 1995 Thu Mar Thu Mar 9 22:25:45 HST 1995
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.
set variable = $< - or - set variable = `head -1`Example:
#!/bin/csh echo -n Input your value: set input = $< echo You entered: $input - or - #!/bin/csh echo -n Input your value: set input = `head -1` echo You entered: $input
This concludes the tutorial. Return to the Table of Contents