views:

177

answers:

2

The standard way to capture command output in Bourne shell is to use the $() syntax:

output=$(mycommand)

For commands that have a lot of output, however, this requires the shell allocate memory for the whole thing as one long string. I'd prefer to find something that does the moral equivalent of the Unix C function popen, to get a new file descriptor I could read from:

newfd=popen(mycommand)
while read -u $newfd LINE; do
  #process output
done

Is this even possible?

+6  A: 
#!bash
ls | while read X
do 
    echo  $X is a directory entry
done

Replace 'ls' with the command of your choice

anon
Completely obvious once you see it. Thanks for the quick response!
Christopher Currie
note the while loop is run in a subshell and so any changes it makes to variables are not passed back to the rest of the script
pixelbeat
A: 

Thanks Neil. Your command helped me solve some other issue.