Bourne Shell Looping



  1. for loop

    The for loop has the following construct:
    for variable in words
    do
      command
      ...
    done
    
    ScriptOutput
    $ cat bl1
    for i in 1 2 3 4 5 6
    do
     echo $i
    done
    
    $ bl1
    1
    2
    3
    4
    5
    6
    
    $ cat bl2
    for i in `date`
    do
     echo $i
    done
    
    $ bl2
    Fri
    Mar
    16
    10:05:18
    CST
    2001
    
    If the list of words is omitted, then the list of words is the command line arguments.
    $ cat bl6
    for i
    do
     echo $i
    done
    
    $ bl6 1 '2 3' 4
    1
    2 3
    4
    


  2. while loop.

    The while construct is :
    while command
    do
      command
      ...
    done
    
    ScriptOutput
    $ cat bl5
    while [ $#  -gt 0 ]
    do
     echo $1
     shift
    done
    
    $ bl5 1 2 '3 4' 5
    1
    2
    3 4
    5
    


  3. until loop.

    The until looping construct is:
    until command
    do
       command
       ...
    done
    
    ScriptOutput
    $ cat bl7
    until [ $#  -eq 0 ]
    do
     echo $1
     shift
    done
    
    $ bl7 123 456 789
    123
    456
    789
    


  4. Examples

    A couple of examples are:

    1. Directory list.

      List the subdirectories in a directory.
      ScriptOutput
      $ cat bl8
      rpath=$1
      files=`ls  $1`
      for entry in $files
      do
        if [ -d $rpath/$entry ]
        then
           echo $rpath/$entry
        fi
      done
      
      $ bl8 Mail
      Mail/drafts
      Mail/inbox
       
      $ bl8 c
      c/acctng
      c/class
      c/fmt
      c/obfuscated
      c/rid
      c/utility
      
      To list directories down in the tree make the script recursive:

      Script

      #!/bin/sh
      rpath=$1
      files=`ls  $1`
      for enrty in $files
      do
        if [ -d $rpath/$entry ]
        then
           echo $rpath/$entry
           ./bl8 $rpath/$entry        
        fi
      done
      

      Output of recurrsive script

      $./bl8 Satan
      Satan/satan-1.1.1
      Satan/satan-1.1.1/bin
      Satan/satan-1.1.1/config
      Satan/satan-1.1.1/html
      Satan/satan-1.1.1/html/tutorials
      Satan/satan-1.1.1/html/tutorials/first_time
      Satan/satan-1.1.1/html/tutorials/vulnerability
      Satan/satan-1.1.1/include
      Satan/satan-1.1.1/include/netinet
      

    2. Multiple occurance of a program

      Run multiple occurrences of a program on different files.

      $ ls /home/kochis/Accounting/Cpr/Wip
      dripro.d        marinevirus.d   molemodlbile.d  speedup.d
      maglevtexmap.d  molecardimed.d  sematech.d      uthscsaphy.d
      

      Script

      $ cat apply
      #!/bin/sh
      for file
      do
         sansd=`echo $file | sed 's/\.d//'`
         echo 1 : $sansd
         key=`echo $sansd | sed 's/^.*\///'`
         echo 2 : $key
         echo 3 : dbtext wd $key $file
         echo
      done 
      

      Output

      $ apply /home/kochis/Accounting/Cpr/Wip/*.d
      1 : /home/kochis/Accounting/Cpr/Wip/dripro
      2 : dripro
      3 : dbtext wd dripro /home/kochis/Accounting/Cpr/Wip/dripro.d
      
      1 : /home/kochis/Accounting/Cpr/Wip/maglevtexmap
      2 : maglevtexmap
      3 : dbtext wd maglevtexmap /home/kochis/Accounting/Cpr/Wip/maglevtexmap.d
      
      1 : /home/kochis/Accounting/Cpr/Wip/marinevirus
      2 : marinevirus
      3 : dbtext wd marinevirus /home/kochis/Accounting/Cpr/Wip/marinevirus.d
      
      1 : /home/kochis/Accounting/Cpr/Wip/molecardimed
      2 : molecardimed
      3 : dbtext wd molecardimed /home/kochis/Accounting/Cpr/Wip/molecardimed.d
      
      1 : /home/kochis/Accounting/Cpr/Wip/molemodlbile
      2 : molemodlbile
      3 : dbtext wd molemodlbile /home/kochis/Accounting/Cpr/Wip/molemodlbile.d
      
      1 : /home/kochis/Accounting/Cpr/Wip/sematech
      2 : sematech
      3 : dbtext wd sematech /home/kochis/Accounting/Cpr/Wip/sematech.d
      
      1 : /home/kochis/Accounting/Cpr/Wip/speedup
      2 : speedup
      3 : dbtext wd speedup /home/kochis/Accounting/Cpr/Wip/speedup.d
      
      1 : /home/kochis/Accounting/Cpr/Wip/uthscsaphy
      2 : uthscsaphy
      3 : dbtext wd uthscsaphy /home/kochis/Accounting/Cpr/Wip/uthscsaphy.d
      
    3. Sum bytes in a directory

      $ ls -l
      total 32
      -rw-r--r--   1 ask      staff         39 Jun 29 08:59 bl2
      -rwxr-xr-x   1 ask      staff        151 Jun 29 09:04 bsum
      -rwxr-xr-x   1 ask      staff         25 Oct 21 1997  testa
      -rwxr-xr-x   1 ask      staff         80 Oct 21 1997  testb
      
      $ ls -s
      total 16       4 bl2       4 bsum      4 testa     4 testb
      

      Script

      $ cat bsum
      #!/bin/sh
      bytes=`ls -l | tr -s ' ' ':' | cut -d: -f5`
      total=0
      for count  in $bytes
      do
       total=`expr $total + $count`
      done
      echo there are $total bytes in this directory
      

      Output of sum script

      $ bsum
      there are 295 bytes in this directory
      
      For debugging use the -x option.

      Ouptut of sum script with trace on

      $ sh -x bsum
      + ls -l
      + tr -s   :
      + cut -d: -f5
      bytes=
      39
      151
      25
      80
      total=0
      + expr 0 + 39
      total=39
      + expr 39 + 151
      total=190
      + expr 190 + 25
      total=215
      + expr 215 + 80
      total=295
      + echo there are 295 bytes in this directory
      

© Allan Kochis Last revision 10/17/2001