Bash - 隐藏命令但不隐藏其输出

Bash - Hiding a command but not its output

我有一个 bash 脚本 (this_script.sh),它调用另一个 TCL 脚本的多个实例。

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

多线程部分摘自 Aleksandr 的回答:Forking / Multi-Threaded Processes | Bash。 该脚本完美运行(仍在尝试找出最后一行)。然而,这一行总是显示:exec tclsh8.5 the_script.tcl "$vars"

如何隐藏该行?我尝试 运行 脚本作为 :

bash this_script.sh > /dev/null

但这也隐藏了调用的 tcl 脚本的输出(我需要 TCL 脚本的输出)。 我尝试将 /dev/null 添加到 for 语句中的语句末尾,但这也不起作用。基本上,我试图隐藏命令而不是输出。

你应该使用 $! 获取刚刚启动的后台进程的 PID,将它们累积在一个变量中,然后 wait 在一秒钟内依次为每个 for循环。

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