The standard unix shells are:
Each shell supports somewhat different functions and shell programming conventions.
Shell scripts are created with any text editor (vi, pico or textedit). Script names should identify the file as a script, e.g. myfile.sh. The first line of an executable shell script should be a special line referencing the shell path:
Shell scripts should include clear comments describing what they do. Comments begin with a # symbol at any point in a line.
The what utility reads special comment lines in text files beginning with the special character string #@(#) so you can include a one-line comment describing your shell script myscript.sh which will be displayed when you issue the command what myscript.sh.
Use the chmod command to change the permissions on your shell script file so that it is executable Then execute it by simply typing its name.
Try writing a simple shell script to create a map frame, a title frame and a legend frame in your GRASS monitor (with d.frame -c frame=map at=bottom%,top%,left%,right% ), then select the map frame (d.frame -s map) and display the Spearfish geology map with vector streams and roads overlaid (d.rast and d.vect), select the legend frame and display the raster legend (d.legend), then select the title frame and put a title in it (d.text < textfile).
Note that your frames are specified by percentages of the monitor height and width. Don't resize your monitor after you create frames, unless you have cleared them all first with d.frame -e.
Unix shells are surprisingly powerful programming environments. You can initialize variables with statements such as:
var=4 name=John list='ls -la'
The echo command displays a defined variable's value: echo $var would return "4" to stdout.
Note in the third example above that the shell would interpret the blank space as separating arguments unless the expression is enclosed in quotes to keep the shell from interpreting the blank. In the Bourne shell, single quotes are more restrictive than double quotes, since double quotes will allow the shell to interpret $ \ and the back-quote character.
A single character following the \ character is interpreted by the shell as a character literal. (This is equivalent to enclosing the single character in quotes.)
The shell interprets a string between back-quote characters as a child shell command which returns some result to the parent shell.
Doing integer math in a shell script requires the expr
function. You can do addition, subtraction, multiplication, division and
modulo division (%). For example, the statement
k=`expr $n \* 2`returns k as double the value of n. The "\" character specifies the following "*" character as a literal (operand) rather than a wildcard.
You can use the Unix infix calculator bc to do more involved math within a shell script.
In the Bourne shell:
if [ test conditions ] then commands else other commands fi
The "else" portion is optional. If-then-else constructs can be nested as you would expect.
The Bourne shell supports an elif (else if) construct as well:
if [ test conditions ] then commands elif [ other test conditions ] then other commands else other commands fi
The Bourne shell supports for, while and until commands for use in looping constructs. (The equivalent C shell commands are foreach, repeat and while). A common shell operation is to initialize and then increment an index variable to control a loop process.
A Bourne shell example:
n=1
while [ $n != 50 ]
do
(statements to be repeated)
n=`expr $n + 1` <--use back-quotes!
done
Note that simply reassigning n=$n+1 doesn't perform the addition, but simply sets n as the character string "1+1" Be careful about spacing in the Bourne shell: don't include spaces when assigning a variable value; do include spaces on either side of an arithmetic operator.
n=1 while [ $n -lt 20 ] do k=`expr $n \* $n` echo $n 'squared is' $k n=`expr $n + 1` done