[Next] [Previous] [Up] [Top] [Contents]

9.9.3 for and foreach

9.9.3.1 Sh

for variable [in list_of_values]

do

command list

done

The list_of_values is optional, with $@ assumed if nothing is specified. Each value in this list is sequentially substituted for variable until the list is emptied. Wildcards can be used and are applied to file names in the current directory. Below we illustrate the for loop in copying all files ending in .old to similar names ending in .new. In these examples the basename utility extracts the base part of the name so that we can exchange the endings.

#!/bin/sh

for file in *.old

do

newf=`basename $file .old`

cp $file $newf.new

done


Introduction to Unix - 14 AUG 1996
[Next] [Previous] [Up] [Top] [Contents]