http://www.csem.duke.edu/Cluster/csh_basics.htm
Contents:
#!/bin/csh # cd data ./runme
As with the csh shell itself, variables may be referenced with a dollar sign. So after we "set p = 2", we can refer to variable p by "echo $p", for example. Note that variables are assumed to be strings unless explicitly operated on with a mathematical operation using the @ functionality (at-sign, see below).
Loops
Here's a simple loop example:
#!/bin/csh # cd data foreach l ( a b c 123 456 red blue green ) runprog < datafile.$l.in > datafile.$l.out end
The foreach line loops through all entries in the paren list, setting the variable l to each one and then executing the loop body. In this case, the loop will be executed 8 times. During the first trip through the loop, variable l will have the value a. The second trip will have l equal to b, etc. When the loop executes with l equal to 123, remember that it is the string "123" and not a number.
Note that we are using input and output redirection, that is, we are using a file to simulate "keyboard input" to the program and we are capturing any screen output to a second file.
#!/bin/csh # cd data foreach l ( a b c 123 456 red blue green ) if( ! -e datafile.$l.out ) then runprog < datafile.$l.in > datafile.$l.out endif end
Finally, the script executes the program, using input and output redirection with the angle brackets (greater-than and less-than signs). Note that if you have done "set noclobber" then you cannot redirect output to an existing file - an error will occur. You can use >> to append the current output to an existing file. If you want to force the overwritting of an existing file, you can also use >!. To redirect the "standard error" output, use >& or >>&.
There is an option for an 'else' section, which will execute if the test condition is false. For several 'If..Then' clauses in a row, you may use 'else if' statements in between, then close the whole group with a single 'endif'.
Note the idea of the above script is that the exact same script can be submitted over and over again and it will ONLY run jobs that have not been completed yet. Once a job has run, it will have produced an output file called "datafile.$l.out" and thus the "if" clause will stop it from being executed again.
Logical Operators
The exclamation point signfies logical NOT - ie. "! -e file" is true if the file does NOT exist. Logical AND is performed with && (double ampersands), logical OR is done with || (double bar).
For numeric values, you can use '>', '>=', '<=', '<'
File-test Operators
There are a number of operators you can use to test different attributes of a file:
Doing Math
To illustrate some math in a script, we'll use the following example:
#!/bin/csh # cd data # foreach p ( 1 2 4 8 16 ) @ s = $p * 25 foreach l ( a b c ) prog -p=$p -size=$s end end
So in this script, the variable "$p" is a string, but we temporarily interpret it as a number, multiply it by 25, and then set that result (as a string) into variable "$s".
'Unix/Linux' 카테고리의 다른 글
ed tutorial (0) | 2014.08.04 |
---|---|
Print IDs of /etc/passwd (0) | 2014.06.11 |
Solaris 9 Volume Manager Problems (0) | 2014.03.21 |
Understanding init scripts (0) | 2014.03.04 |
How To Configure Static IP On CentOS 6 (0) | 2013.12.31 |