Home arrow Shell Programming arrow Write a For loop in shell scripting language
Write a For loop in shell scripting language PDF
Saturday, 23 January 2010

Here is an example of a for loop in FreeBSD's UNIX Shell (sh)

a for loop
  1. #!/bin/sh
  2.  
  3. for i in a b c d
  4. do
  5.         echo $i
  6. done


Another example of a for loop:

a for loop, example 2
  1. #!/bin/sh
  2.  
  3. for i in 1 2 3 4 5
  4. do
  5.         echo $i
  6. done


In third example you can use a for loop to iterate through a list of directories: 

a for loop through a list of directories
  1. #!/bin/sh
  2.  
  3. directories=`ls /`
  4.  
  5. for i in $directories
  6. do
  7.         echo $i
  8. done


 Forth example display numbers from 1 to 20 in a for loop. To generate 20 numbers we use jot command:

Display numbers from 1 to 20 in a for loop using UNIX Shell Scripting
  1. #!/bin/sh
  2.  
  3. numbers=`jot - 1 20`
  4.  
  5. for i in $numbers
  6. do
  7.     echo $i
  8. done

 

 

Last Updated ( Monday, 17 May 2010 )
 
< Prev   Next >

Other BSD Systems

OpenBSD

Misc

Solaris

Polls

Best BSD firewall?