-

Bourne Shell: Chapter 4b

-

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.8 for

    The format for this construct is:
      Command Format:  for loop-index in argument-list            
                            do                                     
                            commands                               
                            done                                   
    
    This structure will assign the value of the first item in the argument list to the loop index and executes the commands between the do and done statements. The do and done statements indicate the beginning and end of the for loop. After the structure passes control to the done statement, it assigns the value of the second item in the argument list to the loop index and repeats the commands. The structure will repeat the commands between the do and done statements once for each argument in the argument list. When the argument list has been exhausted, control passes to the statement following the done. Sample Session: $cat find_henry1 for x in project1 project2 project3 do grep henry $x done Sample Session: $head project? ==> project1 <== henry joe mike sue ==> project2 <== joe mike sue ==> project3 <== joe mike sue henry ==> project4 <== joe mike $find_henry henry henry $ Each file in the argument list was searched for the string, henry. When a match was found, the string was printed.

    4.9 while

    The format for this construct is:
      Command Format:  while expression                            
                            do                                     
                            commands                               
                            done                                   
    
    As long as the expression returns a true exit status, the structure continues to execute the commands between the do and the done statement. Before each loop through the commands, the structure executes the expression. When the exit status of the expression is false (non-zero), control is passed to the statement following the done statement. The commands to be executed must change the expression test or an infinite loop can result.

    4.10 until

    The format for this construct is:
      Command Format:  until expression                            
                           do                                     
                            commands                               
                           done                                   
    
    The until and while structures are very similar. The only difference is that the test is at the top of the loop. The until structure will continue to loop until the expression returns true or a nonerror condition. The while loop will continue as long as a true or nonerror condition is returned. The commands to be executed must change the expression test or an infinite loop can result. Sample Session: $cat until_ex secretname='jenny' name='noname' echo 'Try to guess the secret name!' echo until (test "$name" = "$secretname") do echo 'Your guess: \c' read name done echo 'You did it!' $ The until loop will continue until name is equal to the secret name. Sample Session: $chmod a+x until_ex $until_ex Try to guess the secret name! Your guess: gaylan Your guess: art Your guess: richard Your guess: jenny You did it! $

    4.11 case

    The format for this construct is:
      Command Format:  case test-string in                        
                            pattern-1 ) commands-1 ;;              
                            pattern-2 ) commands-2 ;;              
                            pattern-3 ) commands-3 ;;              
                            .                                      
                            .                                      
                            .                                      
                            *)          commands   ;;              
                       esac                                        
    
    The case structure allows a multiple-branch decision mechanism. The path that is taken depends on a match between the test-string and one of the patterns. Sample Session: $cat case_ex echo 'Enter A, B, or C: \c' read letter case $letter in A) echo 'You entered A' ;; B) echo 'You entered B' ;; C) echo 'You entered C' ;; *) echo 'You did not enter A, B, or C' ;; esac $chmod a+x case_ex $case_ex Enter A, B, or C: B You entered B $case_ex Enter A, B, or C: b You did not enter A, B, or C $ This example uses the value of a character that the user entered as the test string. The value is represented by the variable letter. If letter has the value of A, the structure will execute the command following A. If letter has a value of B or C, then the appropriate commands will be executed. The asterisk indicates any string of characters; and it, therefore, functions as a catchall for a no-match condition. The lowercase b in the second sample session is an example of a no match condition.

    Workshop 4

    This workshop will reinforce your understanding of the ideas presented in Chapter 4. Login to the Multimax using the username and password given to you by the instructor. Each student is to complete the entire workshop. DESK EXERCISES 1. Which utility will evaluate an expression and then return a condition indicating whether or not the expression is true (equal to zero) or false (not equal to zero)? 2. What are the operators for character string comparisons? COMPUTER EXERCISES 3. Use the "if then" construct to write a BourneShell script that will check for at least two parameters being present on the command line. Output an appropriate error message. 4. Write a BourneShell script using the "if then else" construct that will check for equality of two strings that are supplied as parameters to the script. Output a message stating if the strings are equal or not equal. 5. Write a BourneShell script using the "if then elif" construct that will check two numbers, input as parameters, and tell if the first parameter is greater than, equal to, or less than the second number. Output appropriate error messages. 6. Write a BourneShell script using the "for" construct that has a loop index called "fruit" and an argument list as follows: apples oranges bananas pears. Echo the name of each argument to the monitor screen and when the last argument is listed output an appropriate message. 7. Write a BourneShell script using the "while" construct that will add all the numbers between 0 and 9 and display the result. The sum of the digits 0 through 9 is 45. 8. Write a BourneShell script using the "until" construct similar to the example in the manual except compare numbers instead of strings. 9. Write a BourneShell script using the "case" statement that will ask you to enter the day of the week and then echo that day to the monitor screen. Be sure to include an appropriate message if you enter in a string other than a valid day of the week. EXTRA MILE 10. Write a BourneShell script called "dir_num" that will test all of the files in the current directory and print all the files that prove to be a directory.

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