Loop through Arguments: for



next up previous contents index
Next: If-Then-Else Up: Shell Programs (Scripts) Previous: Script Execution and

Loop through Arguments: for

Sometimes we may want to loop through a list of files executing some commands on each file. The for loop is used for this purpose:


for variable in list do statement Execute statement on each loop. done Close the do with done.

If the list option is not given, the for condition loops over the arguments to the shell script.

Now we run bigmat with file names as arguments to the shell command:


#!/bin/sh if [ $# -ne 1 ] then echo "This script needs at least one argument." exit -1 fi for file Loop over the arguments. do echo "Running program bigmat with input $file." bigmat < $file Variable file contains the next argument. done exit



next up previous contents index
Next: If-Then-Else Up: Shell Programs (Scripts) Previous: Script Execution and