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?
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?
You can use awk
to process things on a per-line basis. Exactly what's the best way depends on what you're trying to do though.
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
.
otool -L MyApplication | sed 1d | while read line ;
do
# do stuff with $line
done
Try changing the Internal Field Separator to a newline. By default, bash is going to separate tokens by spaces when using a for loop. Your IFS setting will make the for loop split up the tokens based on whatever string IFS is equal to (thus splitting up the list tokens by newlines instead of by tokens by spaces).
[bash ] $ IFS="
"
[bash ] $ for DEPENDENCY in `otool -L MyApplication | sed 1d`
do
....
done
you can do it awk as well. No need to use a bash for loop
otool -L MyApplication | awk 'NR>1
{
# do your stuff here since awk process line by line.
}'