Wednesday, January 9, 2013

Shell Programming:DECISION-MAKING & LOOP CONSTRUCTS


DECISION-MAKING & LOOP CONSTRUCTS:

* Shell programs can perform conditional tests on their arguments and variables and execute different commands based on the results. For example:

   if [ "$1" = "hyena" ]
   then
     echo "Sorry, hyenas not allowed."
     exit
   elif [ "$1" = "jackal" ]
   then
     echo "Jackals not welcome."
     exit
   else
     echo "Welcome to Bongo Congo."
   fi 
   echo "Do you have anything to declare?"

-- checks the command line to see if the first argument is "hyena" or "jackal" and bails out, using the "exit" command, if they are. Other arguments allow the rest of the file to be executed. Note how "$1" is enclosed in double quotes, so the test will not generate an error message if it yields a null result.
There are a wide variety of such test conditions:

   [ "$shvar" = "fox" ]    String comparison, true if match.
   [ "$shvar" != "fox" ]   String comparison, true if no match.
   [ "$shvar" = "" ]       True if null variable.
   [ "$shvar" != "" ]      True if not null variable.

   [ "$nval" -eq 0 ]       Integer test; true if equal to 0.
   [ "$nval" -ge 0 ]       Integer test; true if greater than or equal to 0.
   [ "$nval" -gt 0 ]       Integer test; true if greater than 0.
   [ "$nval" -le 0 ]       Integer test; true if less than or equal to 0.
   [ "$nval" -lt 0 ]       Integer test; true if less than to 0.
   [ "$nval" -ne 0 ]       Integer test; true if not equal to 0.

   [ -d tmp ]              True if "tmp" is a directory.
   [ -f tmp ]              True if "tmp" is an ordinary file.
   [ -r tmp ]              True if "tmp" can be read.
   [ -s tmp ]              True if "tmp" is nonzero length.
   [ -w tmp ]              True if "tmp" can be written.
   [ -x tmp ]              True if "tmp" is executable.

Incidentally, in the example above:

   if [ "$1" = "hyena" ]

-- there is a potential pitfall in that a user might enter, say, "-d" as a command-line parameter, which would cause an error when the program was run. Now there is only so much that can be done to save users from their own clumsiness, and "bullet-proofing" simple example programs tends to make them not so simple any more, but there is a simple if a bit cluttered fix for such a potential pitfall. It is left as an exercise for the reader.
There is also a "case" control construct that checks for equality with a list of items. It can be used with the example at the beginning of this section:

   case "$1" 
   in
     "gorilla")  echo "Sorry, gorillas not allowed."
                 exit;;
     "hyena")    echo "Hyenas not welcome."
                 exit;;
     *)          echo "Welcome to Bongo Congo.";;
   esac

The string ";;" is used to terminate each "case" clause.
* The fundamental loop construct in the shell is based on the "for" command. For example:

   for nvar in 1 2 3 4 5
   do
     echo $nvar
   done

-- echoes the numbers 1 through 5. The names of all the files in the current directory could be displayed with:

   for file in *
   do
     echo $file
   done

One nice little feature of the shell is that if the "in" parameters are not specified for the "for" command, it just cycles through the command-line arguments.
* There is a "break" command to exit a loop if necessary:

   for file
   do
     if [ "$file" = punchout ]
     then 
       break
     else
       echo $file
     fi
   done

There is also a "continue" command that starts the next iteration of the loop immediately. There must be a command in the "then" or "else" clauses, or the result is an error message. If it's not convenient to actually do anything in the "then" clause, a ":" can be used as a "no-op" command:

   then
     :
   else

* There are two other looping constructs available as well, "while" and "until". For an example of "while":

   n=10
   while [ "$n" -ne 0 ]
   do
     echo $n
     n=`expr $n - 1`
   done

-- counts down from 10 to 1. The "until" loop has similar syntax but tests for a false condition:

   n=10
   until [ "$n" -eq 0 ]
   do
   ...

0 comments:

Post a Comment

Powered by Blogger.