Skip to main content
added 401 characters in body
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k

First a note on style. Using a for loop and unquoted command substitution ($(...) or the ancient `...` form you're using here) to iterate over lines of output is rarely a good idea. TheUnquoted command substitution will split the output of the command on characters of for$IFS loop will split its input(space, tab and newline by default) and perform globbing on whitespacethe result so if you have more than a single space or tab delimited word per line, it will break each into a different variable.You should use And if some of those words contain wildcard characters such as while* instead since this deals with whitespace more gracefully. Also, it is generally preferred to use $(command)? instead, [...], that could be expanded to paths of matching files on the system.

You could use a `command`while IFS= read -r line loop instead to read the input line by line.

That is probably not your problem here though. The main issue is that you are echoing the resultsoutput of lsvgfs ${LINE} for each lineword resulting of resultsthe splitting (and globbing) of the output 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 IFS= read LINE;-r vg; do echoprintf '%s\n' "Volume Group: ${LINEvg}" lsvgfs -- "${LINEvg}" |  while IFS= read LINE2;-r fs; do echoprintf '%s\n' "File System: ${LINE2fs}" df -g $-- "${LINE2fs}"   done echo "" echo  done 

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 

First a note on style. Using a for loop and unquoted command substitution ($(...) or the ancient `...` form you're using here) to iterate over lines of output is rarely a good idea. Unquoted command substitution will split the output of the command on characters of $IFS (space, tab and newline by default) and perform globbing on the result so if you have more than a single space or tab delimited word per line, it will break each into a different variable. And if some of those words contain wildcard characters such as *, ?, [...], that could be expanded to paths of matching files on the system.

You could use a while IFS= read -r line loop instead to read the input line by line.

That is probably not your problem here though. The main issue is that you are echoing the output of lsvgfs ${LINE} for each word resulting of the splitting (and globbing) of the output 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 IFS= read -r vg; do printf '%s\n' "Volume Group: ${vg}" lsvgfs -- "${vg}" |  while IFS= read -r fs; do printf '%s\n' "File System: ${fs}" df -g -- "${fs}"   done  echo  done 
Source Link
terdon
  • 252.6k
  • 69
  • 481
  • 719

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