Saturday, September 16, 2006

An Inline Shell Script with a For Loop

Often I'm asked how to traverse a list of items in a file. You can easily go through a list of items using a for loop. Here's an example of copying selected contents of originalDir to destinationDir via the command line.

#sh
#for i in `cat /home/esofthub/mylist.dat`
#do
#cp -pr /originalDir/$i /destinationDir/.
#echo $i done
#done

sh – shell

cat – lists each item in the list one iteration at a time

cp – the copy utility for a local workstation (plain files and directories, no symbolic links)

-pr – these options preserve the permission, "p" and copies recursively, "r"

echo – lists the item copied

No comments: