For loop with two variable lists?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop with two variable lists?
# 1  
Old 11-22-2010
For loop with two variable lists?

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.
Code:
#!/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

The current output is:
Code:
BACKUP STGPOOL stg1 stg2 copy_stg1 copy_stg2 MAXPR=1

My goal is to processes each variable from the primary_stg and copy_stg matching up their names, see example below.
First loop
Code:
BACKUP STGPOOL stg1 copy_stg1 MAXPR=1

Second loop
Code:
BACKUP STGPOOL stg2 copy_stg2 MAXPR=1

Any help would be greatly appreciated!

Last edited by Franklin52; 11-22-2010 at 02:24 PM.. Reason: Please indent your code and use code tags
# 2  
Old 11-22-2010
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.
# 3  
Old 11-22-2010
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.

Hopefully that makes sense. Smilie
# 4  
Old 11-22-2010
Code:
#!/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

# 5  
Old 11-22-2010
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.

Code:
#!/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

Last edited by Chubler_XL; 11-22-2010 at 09:49 PM..
# 6  
Old 11-23-2010
Try this in bash/ksh93:
Code:
while read ps && read cs <&3
do
  backup "$ps $cs MAXPR=1"
done <<< "$primary_stg" 3<<< "$copy_stg"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

2. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

3. UNIX for Dummies Questions & Answers

Loop and variable not exactly variable: what's wrong

Hello guys, This truly is a newbie question. I'm trying to make a loop to execute simultaneous commands indefinitely while using variable. Here is how my mess looks like (this is just an example): #!/bin/bash IP=`shuf -n 1 IP.txt` # I figured this would be easier to select random lines... (4 Replies)
Discussion started by: bobylapointe
4 Replies

4. Shell Programming and Scripting

printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out. For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do here i want to output the value of... (4 Replies)
Discussion started by: oly_r
4 Replies

5. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

6. Shell Programming and Scripting

For loop with one variable as the name of other

Hello all, I find it hard to explain what I need so I will post the code OVZINCLUDE="16810 16811 1689" PLUS_16810="test" PLUS_16811="test" for VPS in $OVZINCLUDE do echo "Dumping VPSes: $OVZINCLUDE " vzdump --compress --snapshot ${PLUS_$VPS} $VPS done ... (2 Replies)
Discussion started by: click
2 Replies

7. Shell Programming and Scripting

Help with variable using loop

Hi I have webserver that I do read data from. Data are stored like this: Huston |1 Portland |2 Hazen |1 Minneapolis |4 Albany |1 Pittsburg |1 Albany |1 Huston |1 Portland|1 Hazen |2 Albany |2 Huston |1 Hazen |1 Script #!/bin/sh user="admin" (1 Reply)
Discussion started by: Jotne
1 Replies

8. Shell Programming and Scripting

How to make bash wrapper for java/groovy program with variable length arguments lists?

The following bash script does not work because the java/groovy code always thinks there are four arguments even if there are only 1 or 2. As you can see from my hideous backslashes, I am using cygwin bash on windows. export... (1 Reply)
Discussion started by: siegfried
1 Replies

9. Shell Programming and Scripting

Shell Script to Create non-duplicate lists from two lists

File_A contains Strings: a b c d File_B contains Strings: a c z Need to have script written in either sh or ksh. Derive resultant files (File_New_A and File_New_B) from lists File_A and File_B where string elements in File_New_A and File_New_B are listed below. Resultant... (7 Replies)
Discussion started by: mlv_99
7 Replies

10. Shell Programming and Scripting

variable in a for loop

Hi... i am trying to ping my servers .The hostnames are present in a file .They are separated by spaces in the file . i am doing the following : a=1 for name in $(cat host2 |cut -d" " -f$a) do echo Pinging server $name ping -c5 $name a=$a+1 done It is... (3 Replies)
Discussion started by: sars
3 Replies
Login or Register to Ask a Question