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:

open (S,$prog) ||die "Cannot open $prog ($!)n";
open (R,">>","$log") ||die "Cannot open logfile $log!n";

while (<S>) {
    my $date = localtime->strftime('%d.%m.%Y;%H:%M:%S;');
    print "$date$_";
}

I tried to do this in a shell script like this

#!/bin/sh

FILE=/var/log/mylogfile.log

SOCAT=/usr/bin/socat
DEV=/dev/ttyUSB0
BAUD=,b9600
PROG=$SOCAT $DEV$BAUD STDOUT

exec 3<&0
exec 0<$PROG
while read -r line
do
        DATE=`date +%d.%m.%Y;%H:%M:%S;`
        echo $DATE$line >> $FILE
done
exec 0<&3

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:

#!/bin/sh

FILE=/var/log/mylogfile.log

SOCAT=/usr/bin/socat
DEV=/dev/ttyUSB0
BAUD=,b9600
CMD="$SOCAT $DEV$BAUD STDOUT"

$CMD |
while read -r line
do
    echo "$(date +'%d.%m.%Y;%H:%M:%S;')$line" >> $FILE
done

Advertisement

Answer

To read from a process, use process substitution

exec 0< <( $PROG )

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

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

PROG="$SOCAT $DEV$BAUD STDOUT"

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

DATE=$(date '+%d.%m.%Y;%H:%M:%S;')

Moreover, no exec’s are needed:

while ...
    ...
done < <( $PROG )

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