Please contact or Mt Xia for assistance with all your shell programming needs.


  • 4. CONTROL CONSTRUCTS: Go to the bottom, first,previous,next, last section, table of contents.

    4. CONTROL CONSTRUCTS:

    The BourneShell control constructs can alter the flow of control within the script. The BourneShell provides simple two-way branch if statements and multiple-branch case statements, plus for, while, and until statements. In discussing these control structures, the BourneShell keywords will be in bold type and the normal type are the user supplied items to cause the desired effect in command format boxes.

    4.1 Types of Tests Used with Control Constructs:

    The test utility evaluates expressions and returns a condition indicating whether or not the expression is true (equal to zero) or false (not equal to zero). There are no options with this utility. The format for this utility is as follows:
      Command Format:  test expression                             
                                                                   
      expression - composed of constants, variables, and           
                   operators                                       
    
    
    Expressions will be looked at in greater detail later with some examples. There are a few items that need to be mentioned that apply to expressions. Expressions can contain one or more evaluation criteria that test will evaluate. A -a that separates two criteria is a logical AND operator. In this case, both criteria must evaluate to true in order for test to return a value of true. The -o is the logical OR operator. When this operator separates two criteria, one or the other (or both) must be true for test to return a true condition. You can negate any criterion by preceding it with an exclamation mark (!). Parentheses can be used to group criteria. If there are no parentheses, the -a (logical AND operator) takes precedence over the -o (logical OR operator). The test utility will evaluate operators of equal precedence from left to right. Within the expression itself, you must put special characters, such as parentheses, in quote marks so the BourneShell will not evaluate them but will pass them to test. Since each element (evaluation criterion, string, or variable) in an expression is a separate argument, each must be separated by a space. The test utility will work from the command line but it is more often used in a script to test input or verify access to a file. Another way to do the test evaluation is to surround the expression with left and right brackets. A space character must appear after the left bracket and before the right bracket. test expression = [ expression ]

    4.2 Test on Numeric Values

    Test expressions can be in many different forms. The expressions can appear as a set of evaluation criteria. The general form for testing numeric values is:
              int1 op int2                                         
    
    This criterion is true if the integer int1 has the specified algebraic relationship to integer int2. The valid operators (op) are: -eq equal -ne not equal -gt greater than -lt less than -ge greater than or equal -le less than or equal

    4.3 Test on Character Strings

    The evaluation criterion for character strings is similar to numeric comparisons. The general form is: string1 op string2 The operators (op) are: string1 = string2 true if string1 and string 2 are equal string1 != string2 true if string1 and string2 are not equal string1 true if string1 is not the null string Sample Session: $cat test_string number=1 numero=0001 if test $number = $numero then echo "String vals for $number and $numero are =" else echo "String vals for $number and $numero not =" fi if test $number -eq $numero then echo "Numeric vals for $number and $numero are =" else echo "Numeric vals for $number and $numero not =" fi $chmod 755 test_string $sh -x test_string number=1 numero=0001 + test 1 = 0001 + echo String vals for 1 and 0001 not = String vals for 1 and 0001 not = + test 1 -eq 0001 + echo Numeric vals for 1 and 0001 are = Numeric vals for 1 and 0001 are = $test_string String vals for 1 and 0001 not = Numeric vals for 1 and 0001 are = $

    4.4 Test on File Types

    The test utility can be used to determine information about file types. All of the criterion can be found in Appendix B. A few of them are listed here: -r filename true if filename exists and is readable -w filename true if filename exists and is writable -x filename true if filename exists and is executable -f filename true if filename exists and it is a plain file -d filename true if filename exists and it is a directory. -s filename true if filename exits and it contains information (has a size greater than 0 bytes) Example: $test -d new_dir If new_dir is a directory, this criterion will evaluate to true. If it does not exist, then it will be false.

    4.5 if then

    The format for this construct is:
      Command Format:  if expression                               
                            then  commands                         
                       fi                                          
    
    The if statement evaluates the expression and then returns control based on this status. The fi statement marks the end of the if, notice that fi is if spelled backward. The if statement executes the statements immediately following it if the expression returns a true status. If the return status is false, control will transfer to the statement following the fi. Sample Session: $cat check_args if (test $# = 0) then echo 'Please supply at least 1 argument' exit fi echo 'Program is running' $ This little script will check to insure that you are giving at least one argument. If none are given it will display the error message and exit. If one or more arguments are given it will display "Program is running" and run the rest of the script, if any. Sample Session: $check_args Please supply at least 1 argument $check_args xyz Program is running $

    4.6 if then else

    The format for this construct is:
      Command Format:  if expression                               
                            then commands                          
                            else commands                          
                       fi                                         
    
    The else part of this structure makes the single-branch if statement into a two-way branch. If the expression returns a true status, the commands between the then and the else statement will be executed. After these have been executed, control will start again at the statement after the fi. If the expression returns false, the commands following the else statement will be executed. Sample Session: $cat test_string number=1 numero=0001 if test $number = $numero then echo "String values of $number and $numero are equal" else echo "String values of $number and $numero not equal" fi if test $number -eq $numero then echo "Numeric values of $number and $numero are equal" else echo "Numeric values of $number and $numero not equal" fi $ Let's follow the execution of this script with tracing. Sample Session: $sh -x test_string number=1 numero=0001 + test 1 = 0001 + echo String values of 1 and 0001 are not equal String values of 1 and 0001 are not equal + test 1 -eq 0001 + echo Numeric values of 1 and 0001 are equal Numeric values of 1 and 0001 are equal $chmod a+x test_string $test_string String values of 1 and 0001 are not equal Numeric values of 1 and 0001 are equal $

    4.7 if then elif

    The format for this construct is:
      Command Format:  if expression                               
                            then commands                          
                         elif expression                           
                                then commands                      
                                else commands                      
                       fi                                          
    
    
    The elif construct combines the else and if statements and allows you to construct a nested set of if then else structures.

    Go to the top, first,previous,next, last section, table of contents.