Hello everyone, I was hoping for some help with a scripting question.
I am trying to process two lists in a for statement and need to have the lists match up. So far the variables I have match up by line in the file.
So far I have the test script below.
#!/bin/ksh
# for loop to test with two variables
# section to gather the list variables
copy_stg=`cat /tmp/copy_stg.tmp | grep COPY_ | awk '{ print $1 } '`
primary_stg=`cat /tmp/primary_stg.tmp | awk '{ print $1 }'`
for l in $primary_stg
do
backup stgpool $primary_stg $copy_stg MAXPR=1
done
exit
This is interesting. But. What is the goal of your code? Surely not just to create variable names.
The reason I'm asking is that awk does this kind of thing (associative arrays) natively. So a bunch of shell code like this can be written in awk in one or two lines of code.
The goal is using the script to automate backups for Tivoli storage manager. So the script would gather the primary and copy storage pool information using a combination of sql select statements, tivoli command line and field parsing wit hawk and sed. Then issue a backup storage pool command in a loop until using the lists created until all the primary storage pools have been copied. This is why I needed both variables to be tied together so the data is not copied to the wrong storage pool.
#!/bin/ksh
# for loop to test with two variables
# section to gather the list variables
awk '/COPY_/ {print $1}' /tmp/copy_stg.tmp > file1
awk '/COPY_/ {print $1}' /tmp/primary_stg.tmp > file2
paste file1 file2 |while read copy_stg primary_stg
do
backup stgpool $primary_stg $copy_stg MAXPR=1
done
Statements like "matching up their names" and "I needed both variables to be tied together" seem to indicate that we can't assume that item 1 in primary_stg file matches item 1 in the copy_stg file.
Assumption is that "matching up their names" means remove copy_ from copy_stg name and look for a direct match.
#!/bin/ksh
# for loop to test with two variables
# section to gather the list variables
awk '(FNR == NR) { if($0 ~ "COPY_" ) P[$1]++ ; next }
(/COPY_/ && $1 ~ "^copy_") {
if (substr($1,6) in P) printf "%s %s\n", $1, substr($1,6)}
' /tmp/primary_stg.tmp /tmp/copy_stg.tmp |
while read primary_stg copy_stg
do
echo backup stgpool $primary_stg $copy_stg MAXPR=1
done
exit
Current version just echo's the backup command - remove echo if it's OK