Skip to content
Advertisement

Read line output in a shell script

I want to run a program (when executed it produces logdata) out of a shell script and write the output into a text file. I failed to do so :/

$prog is the executed prog -> socat /dev/ttyUSB0,b9600 STDOUT

$log/$FILE is just path to a .txt file

I had a Perl script to do this:

JavaScript

I tried to do this in a shell script like this

JavaScript

Doesn’t work at all…

How do I read the output of that prog and pipe it into my text file using a shell script? What did I do wrong (if I didn’t do everything wrong)?


Final code:

JavaScript

Advertisement

Answer

To read from a process, use process substitution

JavaScript

/bin/sh doesn’t support it, so use /bin/bash instead.

To assign several words to a variable, quote or backslash whitespace:

JavaScript

Semicolon is special in shell, quote it or backslash it:

JavaScript

Moreover, no exec’s are needed:

JavaScript

You might even add > $FILE after done instead of adding each line separately to the file.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement