Foil 02 - Reading from a file and using a for loop

 1 #!/bin/sh
 2 #*********************************************************************
 3 # Sample using a "for" loop to run cmd against a set of users/files. *
 4 # The command is on this command line, the list is in a file.        *
 5 #*********************************************************************
 6 
 7 cmd_to_run="$1"
 8 input_file="$2"
 9 
10 user_list=`cut -d'#' -f1 "$input_file" | grep '^[a-z]'`
11 
12 for name in $user_list
13   do
14     echo "************************** $name **************************"
15     $cmd_to_run $name
16   done
17 
18 #*********************************************************************
19 # Also see the example using a while loop and the read statement to  *
20 # run a set of # commands against a list of client systems,          *
21 # directories, etc. in the remote system maintenance pages.          *
22 #***************************************************************** #02

This example uses operands from the command line to specify the list to be processed and the action to run against the list. As written, only one action can be run against the list, but this can be changed as desired.

  • Lines 7 and 8 assign the passed arguments to variables with meaningful names.
  • Line 10 reads the input file, extracting anything to the left of the "#" comment delimiter. Only lines starting with a through z are processed. The list is assigned to a variable.
  • Lines 12, 13, and 16 define the loop using the for command to process the list.
  • Line 15 executes the command against one item in the list.

    Previous   Next   Index