user2883071
user2883071

Reputation: 980

Bash - Hiding a command but not its output

I have a bash script (this_script.sh) that invokes multiple instances of another TCL script.

set -m
for vars in $( cat vars.txt );
do
   exec tclsh8.5 the_script.tcl "$vars" &
done
while [ 1 ]; do fg 2> /dev/null; [ $? == 1 ] && break; done

The multi threading portion was taken from Aleksandr's answer on: Forking / Multi-Threaded Processes | Bash. The script works perfectly (still trying to figure out the last line). However, this line is always displaed: exec tclsh8.5 the_script.tcl "$vars"

How do I hide that line? I tried running the script as :

bash this_script.sh > /dev/null

But this hides the output of the invoked tcl scripts too (I need the output of the TCL scripts). I tried adding the /dev/null to the end of the statement within the for statement, but that too did not work either. Basically, I am trying to hide the command but not the output.

Upvotes: 0

Views: 370

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137627

You should use $! to get the PID of the background process just started, accumulate those in a variable, and then wait for each of those in turn in a second for loop.

set -m
pids=""
for vars in $( cat vars.txt ); do
   tclsh8.5 the_script.tcl "$vars" &
   pids="$pids $!"
done
for pid in $pids; do
   wait $pid
   # Ought to look at $? for failures, but there's no point in not reaping them all
done

Upvotes: 1

Related Questions