Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

OS: AIX 7100-04-1216

So basically I'm trying to write a for loop that sees which volumegroups I have on my system, which filesystems reside in those volume groups and what the size of each of those filesystems is.

I have following code:

for LINE in `lsvg` ; do
    echo "Volume Group: "${LINE}
    for LINE2 in `lsvgfs ${LINE}` ; do
        echo "`lsvgfs ${LINE}` \n"
        df -g ${LINE2}
    done
done

The output of lsvg

rootvg
nimvg

The output of lsvgfs (for rootvg)

/
/usr

The output of lsvgfs (for nimvg)

/export/nim/lpp_source
/export/nim/spot 

The output of df -g (for / in volumegroup rootvg)

Filesystem    GB blocks      Free %Used    Iused %Iused Mounted on
/dev/hd4           5.25      2.86   46%     9957     2% /

The output of df -g (for /usr in volumegroup rootvg)

Filesystem    GB blocks      Free %Used    Iused %Iused Mounted on
/dev/hd2           2.00      0.17   92%    43194    50% /usr

The output of df -g (for /export/nim/lpp_source in volumegroup nimvg)

Filesystem    GB blocks      Free %Used    Iused %Iused Mounted on
/dev/fslv02       10.00      8.24   18%      597     1% /export/nim/lpp_source

The output of df -g (for /export/nim/spot in volumegroup nimvg)

Filesystem    GB blocks      Free %Used    Iused %Iused Mounted on
/dev/hd4           5.25      2.86   46%     9957     2% /

What it should do:

Volume Group: rootvg
File System: /
/dev/hd4           5.25      2.86   46%     9957     2% /
File System: /usr
/dev/hd2           2.00      0.17   92%    43194    50% /usr
File System: /var

Volume Group: nimvg
File system: /export/nim/lpp_source
/dev/fslv02       10.00      8.24   18%      597     1% /export/nim/lpp_source
File system: /export/nim/spot 
/dev/hd4           5.25      2.86   46%     9957     2% /

What I get:

Volume Group: rootvg
/
/usr
Filesystem    GB blocks      Free %Used    Iused %Iused Mounted on
/dev/hd4           5.25      2.86   46%     9957     2% /
/
/usr
Filesystem    GB blocks      Free %Used    Iused %Iused Mounted on
/dev/hd2           2.00      0.17   92%    43194    50% /usr


Volume Group: nimvg
/export/nim/lpp_source
/export/nim/spot 
Filesystem    GB blocks      Free %Used    Iused %Iused Mounted on
/dev/fslv02       10.00      8.24   18%      597     1% /export/nim/lpp_source
/export/nim/lpp_source
/export/nim/spot 

Filesystem    GB blocks      Free %Used    Iused %Iused Mounted on
/dev/hd4           5.25      2.86   46%     9957     2% /
share|improve this question
1  
Please always include your OS. Solutions very often depend on the Operating System being used. Are you using Unix, Linux, BSD, OSX, something else? Which version? –  terdon Jul 14 '14 at 12:48
    
I have added my OS, although I don't see what difference it makes because it is just a for loop? –  Timothy Persoon Jul 14 '14 at 12:52
    
You never know. Your problems could be related to shell buffering for example which might behave differently on different systems. It will certainly depend on the shell being used (I assume you're using bash, right?). Also, the -g option for df doesn't exist on GNU df so that was confusing as is your echo \n which will print a literal \n (not a newline) on all systems I am familiar with. Does it work as expected on AIX? Without the -e flag? Finally, where does File System come from? It does not appear in the output examples or your loop. –  terdon Jul 14 '14 at 12:56
    
File System is something that should be added like echo "Volume Group: "${LINE}. Yes the echo \n does what it should do and the -g flag is just so the data is formatted in GB. –  Timothy Persoon Jul 14 '14 at 13:02

1 Answer 1

up vote 2 down vote accepted

First a note on style. Using a for loop to iterate over lines of output is rarely a good idea. The for loop will split its input on whitespace so if you have more than a single word per line, it will break each into a different variable.You should use while instead since this deals with whitespace more gracefully. Also, it is generally preferred to use $(command) instead of `command`.

That is probably not your problem here though. The main issue is that you are echoing the results of lsvgfs ${LINE} for each line of results of lsvgfs ${LINE}:

for LINE2 in `lsvgfs ${LINE}` ; do
        echo "`lsvgfs ${LINE}` \n"
done

So, of course, you're getting these lines twice:

/
/usr

Try this instead:

lsvg | while read LINE; do
    echo "Volume Group: ${LINE}"
    lsvgfs "${LINE}" | while read LINE2; do
        echo "File System: ${LINE2}"
        df -g ${LINE2}
    done
echo ""
done
share|improve this answer
    
Works like a charm! Since I'm a beginner in this kind of scripting, it's nice to see an explanation with what was wrong. Thanks –  Timothy Persoon Jul 14 '14 at 13:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.