我想要一个简单的测试 shell 脚本,它可以并行启动程序 N 次,并将每次不同的输出保存到不同的文件中。我已经开始并行启动程序并保存输出,但我怎样才能只保留不同的输出?另外,我怎样才能让 echo DONE!
指示结束?
#!/bin/bash
N=10
for((i=1; j<=$N; ++i)); do
./test > output-$N &
done
echo DONE!
最佳答案
您需要使用内置的 wait。
wait [n ...]
Wait for each specified process and return its termination status. Each n may be a process ID or a job specification; if a job spec is given, all processes in that job’s pipeline are waited for. If n is not given, all currently active child pro- cesses are waited for, and the return status is zero. If n specifies a non-existent process or job, the return status is 127. Otherwise, the return status is the exit status of the last process or job waited for.
您可以将作业指定为 %1
、%2
、...:
wait %1 %2 %3 ...
但是只要你没有其他子进程,你就可以不带参数地使用它;然后它将等待所有子进程完成:
for ...; do
...
done
wait
echo "All done!"
您的另一个问题,即如何只保留不同的输出,有点棘手。您到底是什么意思-与什么不同?如果您有基线,您可以这样做:
for ...; do
if diff -q $this_output $base_output; then
# files are identical
rm $this_output
fi
done
如果你想保留所有唯一的输出,算法显然会稍微复杂一些,但你仍然可以使用 diff -q
来测试相同的输出。
关于bash - 用于测试的shell脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2959413/