I may simply not know how to phrase what I am asking but I haven't been able to get the answers that I am looking for.
So what I am trying to to is run a command or script within a bash script capture the output line by line and check it against some value.
I have tried things to the effect of
#!/bin/bash
./runningscript.sh | while read line; do
echo "output $line"
done
and
#!/bin/bash
./runningscript.sh | {read line echo "output $line"}
both just seem to execute the script giving me the normal output. What I want is to handle each line of output from runningscript.sh from within this bash script as it is output I don't want it to wait until runningscript.sh is finished running to handle it.
Sorry I am a very occasional and simple bash user so this may be the stupidest question to ask but any help would be greatly appreciated
Your scripts work. but the output doesn't appear until late, maybe even after the commands are done? This is typically because of buffering: runningscript.sh
has to fill up a buffer, typically 4 kB, before its output is transferred through the pipe to the next process in the pipeline.
A fix is to use the unbuffer
command which is part of the expect
package:
unbuffer runningscript.sh | something_else
This tricks runningscript.sh into thinking it is writing to an interactive terminal. As a result, it doesn't buffer.
For more, see https://unix.stackexchange.com/questions/25372/turn-off-buffering-in-pipe
Alternatively, if you have GNU coreutils 7.5 or better, you can disable output buffering with the stdbuf command:
stdbuf -oO runningscript.sh | something_else
The form I see most often is:
while read line; do
stuff to $line
done < $(somescript.sh)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With