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?
From stackoverflow
-
#!bash ls | while read X do echo $X is a directory entry doneReplace 'ls' with the command of your choice
Christopher Currie : Completely obvious once you see it. Thanks for the quick response!pixelbeat : 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 -
Thanks Neil. Your command helped me solve some other issue.
0 comments:
Post a Comment