How do I capture the output of a command to a file descriptor in Bourne shell?
Question
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?
Solution
#!bash
ls | while read X
do
echo $X is a directory entry
done
Replace 'ls' with the command of your choice
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow