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)

3 Answers 3

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
6
  • im getting back to the command line when one of the "inner" scripts is done... Commented Nov 11, 2016 at 22:04
  • @muhamadli sorry about that I put and gates when I should have done or gates. Updated my answer. Commented Nov 11, 2016 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? Commented Nov 11, 2016 at 22:15
  • I see, that makes sense. I updated my answer to only tail out the last line per clear. Commented Nov 11, 2016 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 Commented Nov 11, 2016 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    

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.