Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

286
Views
Bash capture output of running script

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

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report

0

The form I see most often is:

while read line; do
    stuff to $line
done < $(somescript.sh)
over 4 years ago · Santiago Trujillo Report

0

For this you have to edit runningscript.sh and put echo or whatever action you want put it in its own loop. Piping cannot help you because first it completes its first program then puts it output to the second. If you improve your runningscript.sh, you will not need to use extra script.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!