Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

shell

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?

like image 735
Dave Mateer Avatar asked Jun 24 '10 18:06

Dave Mateer


People also ask

How commands are processed by the shell?

The shell parses the command line and finds the program to execute. It passes any options and arguments to the program as part of a new process for the command such as ps above. While the process is running ps above the shell waits for the process to complete.

How do I process a file line by line in bash?

In Bash, you can use a while loop on the command line to read each line of text from a file and do something with it. Our text file is called “data. txt.” It holds a list of the months of the year. The while loop reads a line from the file, and the execution flow of the little program passes to the body of the loop.


2 Answers

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.

like image 94
tonio Avatar answered Nov 04 '22 13:11

tonio


otool -L MyApplication | sed 1d | while read line ; do   # do stuff with $line done 
like image 44
Jim Lewis Avatar answered Nov 04 '22 12:11

Jim Lewis