Saturday 12 March 2016

How do you process the output of a command in the shell line-by-line?



I need to process the shared library dependencies of a library from a bash script. The for command processes word-by-word:



for DEPENDENCY in `otool -L MyApplication | sed 1d`
do
...

done


What is the way to process the results line-by-line?


Answer



You should use the read command.



otool -L MyApplication | sed 1d | \
while read i
do

echo "line: " $i
done


See bashref for a description of the read builtin, and its options. You can also have a look at the following tutorial for examples of using read in conjunction with for.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...