1753726 Members
4374 Online
108799 Solutions
New Discussion

For loop with two vars

 
SOLVED
Go to solution
Paul Sperry
Honored Contributor

For loop with two vars

I have 2 files

file 1

1999
2000

 

File 2

12457
12458

 

and 2 directories

ls -al tars_1999

drwxr-xr-x   2 15709 15709   101 Jul  7  2011 12457_1999

drwxr-xr-x   2 15709 15709   101 Jul  7  2011 12458_1999

 

ls -al tars_2000

drwxr-xr-x   2 15709 15709   101 Jul  8  2011 12457_2000
drwxr-xr-x   2 15709 15709   101 Jul  8  2011 12458_2000

 

I would like out put to be

file2 number, 1999 size, 2000 size

 

I have tried:

 

for x in `more file1`
        do
                for y in `more file2`
                do
                        du -sh tars_$x/$y_$x
                done
        done

but it dosen't pick up the $y

 

also tried

n=1999
for x in 'more file2'
do
        du -sh tars_$n/$x_n/*
        n=$(( $n + 1 ))
done

 

but the n never changes.

 

Of cource this is just a small sample of the data I have

 

any ideas..

TIA

 

4 REPLIES 4
Patrick Wallek
Honored Contributor
Solution

Re: For loop with two vars

The better way to do it is:

 

for x in $(< file1)
        do
                for y in $(< file2)
                do
                        du -sk tars_${x}/${y}_${x}
                done
        done

 

 

Also note that HP-UX does not have the '-h' option with 'du'.  So 'du -sh' has been changed to 'du -sk' above.

Paul Sperry
Honored Contributor

Re: For loop with two vars

Thanks that works great.

Is the any way to output the results to a file

other than ./scriptname > outputfile

 

I guess I am looking for something with in the script to create the output file

Thanks

Patrick Wallek
Honored Contributor

Re: For loop with two vars

You can always just redirect the output of the 'du' command.

 

'du -sk ...' >> outputfile

 

If you do that, just be sure to append (with the >>) so you don't overwrite the file everytime.

Dennis Handly
Acclaimed Contributor

Re: For loop with two vars

>You can always just redirect the output of the 'du' command.  'du -sk ...' >> outputfile

 

You can also redirect the whole loop:

for x in $(< file1); do
   for y in $(< file2);   do

      du -kxs tars_${x}/${y}_${x}

   done

done > outputfile