Thursday 16 May 2019

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

Answer


Answer




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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...