Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

while and for loop with variables problem

Status
Not open for further replies.

jboo

Programmer
Jan 4, 2001
12
GB
I've a 2 suite set of programs that runs via a Unix script with its entries:
run PROG01
D01 (Depot)
R12 (Rep No.)
run PROG02
Tom (Print recipient 1)
Dick (Recipient 2)
[Space] (signify end of recipient list)

We've now got 30 depots and lots of reps so the script consists, now, of the above lines

repeated many, many times. I'd like to put the processing in loop(s) but I'm struggling. The

rep nos. follow no sequence and the recipients are varied, both in name and number. For

example, we want all depots, but we want reports for say, depot 1, rep. 12 to go to Tom and

Fred, depot 1, rep.34 to go to Fred, depot 2, rep. 11 to go to Bill, Dick and Harry, depot

2, rep. 13 to go to Tom and so on and so on.

Can it be done in a number of embedded loop? Have I given a clear enough explanation?
 
I'm not clear on exactly what you are trying to do.
I'm thinking that you want to read lines from
a field delimited configuration file, set values for
some variables like DEPOT, REP_NO, RECIPIENTS, etc.,
then run some commands, then repeat this process
with the next line in the config file until you are done.

If this is true,
here's an example using a while loop and awk.

sample config file:
PROG01:D01:R12:TOM;DICK

#!/usr/bin/ksh
CONFIG_FILE=/tmp/config.file

cat $CONFIG_FILE|while read LINE
do
PROGRAM=`echo $LINE|awk '{print $1}'`
DEPOT_NO=`echo $LINE|awk '{print $2}'`
REPORT_NO=`echo $LINE|awk '{print $3}'`
RECIPIENTS=`echo $LINE|awk '{print $4}'`

echo "Program: $PROGRAM"
echo "Depot: $DEPOT_NO"
echo "Report: $REPORT_NO"
echo "Recipients: $RECIPIENTS"
done

The example uses echo statements,
but can alter the script to instead run commands
and use the variables as arguments.

Robert
Robert G. Jordan
Unix Sys Admin
Sleepy Hollow, Illinois U.S.A.
sh.gif


FREE Unix Scripts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top