0

I am trying to print 2 outputs from different scripts that run in the background in the same time with a third one.

My third script looks like:

#!/bin/bash
./script1 &
./script2 &

The output of every script is a simple progress bar:

scriptN: [##....................] (10%) (10/100)

I would like the output to be something like that:

script1: [##....................] (10%) (10/100)
script2: [##....................] (10%) (10/100)
0

I don't think this is possible, at least not without enormous effort. Dynamic progress bars in the terminal rely on control characters to change the position of the cursor and redraw characters on the screen. Running two programs simultaneously that each attempt to control what's being drawn will result in conflicts between the two programs.

It may be possible to write a script that emulates a terminal and composes each output stream independently and then recomposes them together into a single stream, but I've not heard of anybody attempting such a thing before.

0

This is kinda hacky and will spam your screen so scrolling back won't be easy but should work.

#!/bin/bash
output1=$(mktemp /tmp/tmpout.XXXXXX)
output2=$(mktemp /tmp/tmpout.XXXXXX)
./script1 &> $output1 &
pid1=$!
./script2 &> $output2 &
pid2=$!

while [ -d "/proc/$pid1" ] || [ -d "/proc/$pid2" ]
do
   clear
   tail -n 1 $output1
   tail -n 1 $output2
   sleep 1
done
rm -f $output1 $output2
  • im getting back to the command line when one of the "inner" scripts is done... – muhamad li Nov 11 '16 at 22:04
  • @muhamadli sorry about that I put and gates when I should have done or gates. Updated my answer. – Zachary Brady Nov 11 '16 at 22:05
  • changed the code to #!/bin/bash output1=$(mktemp /tmp/tmpout.XXXXXX) output2=$(mktemp /tmp/tmpout.XXXXXX) ./script1 &> $output1 & pid1=$! ./script2 &> $output2 & pid2=$! while [ -d "/proc/$pid1" ] || [ -d "/proc/$pid2" ] do out1="$(cat $output1)" out2="$(cat $output2)" echo -ne "\r$out1\n$out2" sleep 1 done rm -f $output1 $output2 my output is: script1: [##....................] (10%) (10/100) script1: [###..................] (20%) (20/100) script1: [####..................] (30%) (30/100) script2: [##....................] (10%) (10/100) - keeps going help? – muhamad li Nov 11 '16 at 22:15
  • I see, that makes sense. I updated my answer to only tail out the last line per clear. – Zachary Brady Nov 11 '16 at 22:18
  • still printing both of the lines one after the other script1: [##....................] (10%) script2: [##....................] (10%) script1: [##....................] (11%) script2: [##....................] (11%) .. and so on. im looking for something more like echo -ne that will stay at the same line and update itself – muhamad li Nov 11 '16 at 22:29
0

worked after a small research and the help of @ZacharyBrady

#!/bin/bash
output1=$(mktemp /tmp/tmpout.XXXXXX)
output2=$(mktemp /tmp/tmpout.XXXXXX)
./script1 &> $output1 &
pid1=$!
./script2 &> $output2 &
pid2=$!

while [ -d "/proc/$pid1" ] || [ -d "/proc/$pid2" ]
do
    out1="$(tail -n 1 $output1)"
    out2="$(tail -n 1 $output2)"
    echo $out1
    echo $out2
    tput cuu1  # move cursor up by one line
    tput el # clear the line
    tput cuu1
    tput el
done
rm -f $output1 $output2    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.