Home‎ > ‎CIS 98‎ > ‎

Looping

Presentation slides are here.

Another characteristic of programming languages is the ability to repeatedly execute a block of code. Such a construct is called a looping construct because you execute the same code over and over again until some terminating condition is met. The Bourne shell uses the following statements to implement program looping:
  • while / do / done
  • until / do /done
  • for / do /done
  1. Objectives
    • Use while and until statements to execute a section of code repeatedly
    • Use the for statement to execute a section of code a known number of times
    • Control the execution of a loop
  2. The while Statement
    • Syntax
      while boolean statement
      do
      	command-list
      done
      Note: the boolean statement must evaluate to true or false.
      The code from command-list is executed only when the boolean statement evaluates to true.
    • Nesting While loops:
      # Display several files with line numbers
      while [ "$#" -gt 0 ]
      do
      	LINE_NUM=1
      	cat $1 | while read LINE
      	do
      		echo "$LINE_NUM $LINE"
      		LINE_NUM=`expr "$LINE_NUM" + 1`
      	done
      	shift
      done
    • Exercise 1
  3. The until Statement
    • Syntax
      until boolean statement
      do
      	command-list
      done
      Note: the boolean statement must evaluate to true or false.
      ] The code from command-list is executed only when the boolean statement evaluates to false.
    • Example:
      # Call upon a user as soon as he/she logs in
      if [ "$#" -ne 1 ]
      then
      	echo "usage: $0 username" >&2
      	exit 1
      fi
      until who | grep "$1 " > /dev/null 2>&1
      do
      	echo -n .
      	sleep 1
      done
      # The user has logged in.  Allow time to complete login process
      sleep 20
      write $1 < "$1, Please come see me in my office. -$LOGNAME"
  4. The for Statement
    • Syntax
      for name in string1 string2 string3 ...
      do
      	command-list
      done
      The variable name will be iteratively assigned the values of string1, string2, string3 etc. The loop terminates after the last value ofname is run through the command-list
    • The string values must be separated by white space
    • A string representing a filename can use shell wildcards
    • At least one string must follow the keyword "in"
    • Use a backslash (\) for line continuation if all strings do not fit \
      on one line.
    • Example:
      # Append an extension to a bunch of text files
      for FILE in *
      do
      	mv $FILE ${FILE}.txt
      done
    • Exercise 2
    • An alternate form of the for loop:
      for name 
      do
      done

      Without the "in" clause, the variable name will iterate through all the positional parameters.
  5. Loop Commands
    There are two commands available as shell built-ins that affect the control of flow within a loop.
    1. The break statement: break [n]
      Causes execution to leave the loop by jumping directly to the done statement
    2. The continue statement: continue [n] 
      Causes execution of the loop to jump back to the do statement after checking the bollean statement again.
    3. Example:
      # Renames filesnames specified on the command line with standard extensions
      for FILE
      do
      	echo -n "With which extention do you want to rename $FILE?
      	t) txt   c) conf  d) defs  s) so  n) no extension
      (q to quit) ==> "
      	read REPLY DUMMY
      	case "$REPLY" in
      	   [tT] )   EXT=txt ;;
      	   [cC] )   EXT=conf ;;
      	   [dD] )   EXT=defs ;;
      	   [sS] )   EXT=so ;;
      	   [nN]*)   continue ;;
      	   [qQ]*)   break ;;
      	       *)   echo "Invalid response. Bye!" >&2
      		    exit 1 ;;
      	esac
      	mv "$FILE" "$FILE.$EXT" && echo "$FILE renamed to $FILE.$EXT"
      	LIST="$LIST $FILE.$EXT"
      done
      echo "Summary: These files have been renamed:"
      ls -l $LIST

Exercise 1: Using the while statement

Write a block of code that will print out multiple copies of a particular file. The filename will be the first argument to the shell script, the number of copies will be the second. Supply the missing code:
> tmp$$   # Create a temporary file
COUNT=0
...               # Your code goes here
lp tmp$$
rm tmp$$

Exercise 2: Unexpected errors

Explain why the following commands produce the error message they do.
$ ls
letter   memo      report   status
$ mv * *.txt
mv: *.txt not found
Comments