Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to evaluate a stream line by line

Tags:

bash

shell

unix

I am trying to avoid creating any new files to store output in order to minimize the risk of overwriting something in a directory with the same name. I am trying to just evaluate each line in a stream with a pipe instead of outputting to a file and then using a while read line do done < file loop. Something like:

echo -e "1\n2\n3\n4\n5" | #evaluate current line separately#

Could I somehow read each line into an array and then evaluate the elements in the array? or is there a better way to avoid accidentally overwriting files?

like image 259
bqui56 Avatar asked Aug 03 '12 08:08

bqui56


People also ask

What is stream function ψ?

Stream functions are defined for two-dimensional flow and for three-dimensional axial symmetric flow. The stream function can be used to plot the streamlines of the flow and find the velocity. For two-dimensional flow the velocity components can be calculated in Cartesian coordinates by. (10.5)

What are stream lines?

A streamline is a line that is tangential to the instantaneous velocity direction (velocity is a vector, and it has a magnitude and a direction). To visualize this in a flow, we could imagine the motion of a small marked element of fluid.

What is the difference between streamline and stream function?

Because a streamline is always tangent to the velocity at a every point along its length, there can be no flow across a streamline. The mathematical equation that describes the streamline in any flow is called a stream function.


3 Answers

In bash, the common way is to use the Process Substitution:

while read line ; do
    ...
done < <( commands producing the input)
like image 66
choroba Avatar answered Oct 30 '22 02:10

choroba


You were halfway there...

echo -e "1\n2\n3\n4\n5" | while read line; do
    ...
done

Note that bash runs each part of the pipeline in a separate process, and any variables defined there will not persist after that block. (ksh93 will preserve them, as the loop will run in the current shell process.)

like image 26
Henk Langeveld Avatar answered Oct 30 '22 02:10

Henk Langeveld


You can avoid overwriting files by using mktemp or tempfile to create temporary files with unique names. However, I would use process substitution as in choroba's answer.

like image 1
Dennis Williamson Avatar answered Oct 30 '22 04:10

Dennis Williamson