Bourne Shell Programming Variables - creating and assigning variables Bourne shell use the set and unset to create and assign values to variables or typing the variable name, an equal sign and the value to which you would like it assigned makes an assignment. If a variable of that name es not already exist in the current shell, this will create that variable in the current shell. If a variable of that name es already exist in the current shell, this will change its value. The method for knowing which variables are local and which are environmental in the Bourne shell uses the env command. In the Bourne shell, this information is managed by the "export list". The export list contains the list of variables that will be given to any shells started as children of the current shell. To put a variable on the export list, type the command export with a variable name as the argument. name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable. echo, read and expr In Bourne shell, if you request the display of a variable which es not exist, there will be no error message. Instead, it will display as if that variable has no value (is blank). Another difference in the echo command is that the -n option in C-shell which prevents the end of line is replaced by a \c at the end of the string. For input the Bourne shell uses the read utility which allows user input to be assigned into the variable given as the parameter. The expr command is used to evaluate a mathematical expression. #!/usr/bin/sh #This script is to show how echo is different from the C-shell version # how the read command can be used for input # and how the expr command can be used to evaluate an expression echo "Enter an integer: \c" read int1 echo "Enter another integer: \c" read int2 int3=`expr $int1 + $int2` echo "Sum = $int3 and Difference = `expr $int1 - $int2`" echo "That is all" All BASH shell scripts should include the magic line as the very rst line in the test le which tells the system which shell to use to evaluate the included statements: #!/bin/bash <option> Ou can also invoke debugging options interactively, within the script or with the set command: set e If a simple command fails the shell shall immediately exit set n The shell shall read but es not execute them set u The shell shall write a message to standard error when it tries to expand a variable that is not set and immediately exit set v The shell shall write its input to standard error as it is read set x The shell shall write to standard error a trace for each command after it expands the command and before it executes it.
command line arguments The items typed on the command line are put into a vector similar to the method used in the C-shell. The name of the command used to run the script is put into the variable by the name $0 and its arguments are put into the next elements which are named $1 $9. The $1 $9 are actually called the "positional parameters". If there were more than 9 parameters given on the command line, the shift command must be used to bring them into these rst 9 positions before they can be accessed. In Bourne shell, there is a set command, it just has a different purpose than the set command of the C-shell. The set command here allows you to assign values into the positional parameters. Giving the command followed by a list of values will put those values into the rst positions of the positional parameters. Note you can also unset variable (undened them). set Jan Mike echo $1 Jan echo $2 Mike This can be especially useful because it is an easy way to create an array of elements when those elements are normally only on a line separated by spaces. For example, the output of the date command has many pieces of information separated from one another by spaces. Assigning this into the positional parameters allows you to access each element individually. set `date` echo $1 Fri echo $6 2002 PATH UNIX / LINUX executables are located in a directory sequence called the PATH (echo $PATH). Unlike Winws which searches rst in the current directory for an executable, UNUX and LINUX only use the PATH vaiable to locate executable.
conditionals The structure of the if conditional is quite different from the structure in the C-shell programming language, but its purpose and parts are the same. In both languages, the if must have a condition to test and a group of shell that will be performed if and only if that expression evaluates to true. In the Bourne shell the condition being evaluated is called a "test condition". This is probably because it is often with the utility named test. But anything that returns the successful/unsuccessful result can be used. The true (or successful) is assumed to be the value 0, and the false (or unsuccessful) is any non-zero value. test The test utility, has many formats for evaluating expressions. For example, when given three arguments, will return the value true if the rst and third argument hold the relation represented by the second argument, but it also has many options to test other properties of les, strings and integers. The test utility can be used by stating the keyword test which is followed by one of its many expression formats, or the test utility can simply be called by putting the expression in square brackets. Some of the possible expressions are: int1 relational-operator compares the integers on either side according to the relational operator int2 (-lt,-gt, -eq, -ne, -le, -ge) string1 = string2 compares the two strings for equality string1!= string2 compares the two strings for inequality -z string true if the string is of length 0 -r lename true if you have read permission on the le named There are also compound expressions allowed in the test expression. These are built by using the -a for "and" and the -o for "or". You can also use parentheses to set the order of precedence and the exclamation point for not. if - - The structure of the if (one without an ) has the if and the expression to be tested on one line, followed with a on a line by itself, followed by the body, and nally followed by a (if written backwards). if test-expression or if [condition] The test expression will include everything between the keyword if and the end of that line. Like the if in the C-shell, the in the body are each complete lines as they would be given on the shell command line (Bourne shell command line in this case). The of the body are in sequence as listed. adding the clause The structure of the if with an has the keyword if and the expression to be tested on one line, followed by the on a line by itself, followed by the body to be if the condition is true, followed by an on a line by itself, followed by the body to be if the condition is false, followed by a on its own line. if test-expression or if [condition] 1 2
adding the elif clause There is also the possibility of the having an if (like there was in C-shell), but in Bourne shell, it is not written as two separate words. The "" and the "if" are combined to make an elif keyword. The elif keyword is used just like the " if" of the C-shell in that it takes its own condition but es not need its own end marker. if test-expression1 or if [condition1] 1 elif test-expression2 or if [condition2] 2 3 #!/usr/bin/sh # To show the use of read and structured if echo -n "rst value: " read val1 echo -n "second value: " read val2 echo -n "third value: " read val3 if test $val1 = $val2 -a $val1 = $val3 echo "complete match" elif test $val1 = $val2 -o $val1 = $val3 -o $val2 = $val3 echo "only two match" echo "no matches"
loops In Bourne shell there are several loop structures. The two for looping structures are similar to the foreach of C-shell programming and the while and until loops are more similar to the while loop of C-shell programming. In all of the loop structures of Bourne shell, the body of the loop is contained between a (on a line by itself) and a (also on a line by itself). The in the body are any valid Bourne shell and will be performed in sequence. for - in The for-in loop structure uses a loop control variable specied after the keyword for. The loop control variable takes on the values given in the list which is specied after the keyword in. This structure is very similar to the foreach loop of C-shell. The difference is the use of the word in rather than the parenthesis to separate the loop control variable from the list of values. for loop-var in list for The for loop is actually a for-in loop but using a default value for the list rather than specifying it explicitly. The default value for the list is the argument vector (as it was called it C-shell) or the command line parameter list (as it is called in Bourne shell). This is the most common use of the for statement is to be able to traverse the list of command line parameters. for loop-var while The while loop uses the same structure for the test-command as was used in the if statement. The statements in the body of the while loop are performed completely and in sequence if the test-command evaluates to true. while test-command until The until loop has the same structure as the while in that it is top-tested. (Unlike the "-while" loop of the C programming language.) The while and the until are only different in the condition specied at the top. The while loop will go into and perform the statements in the body of the loop if and only if that condition evaluates to true. With the until loop, the statements are performed if and only if the expression evaluates to false. until test-command