bash - 用于测试的shell脚本

标签 bash testing shell scripting

我想要一个简单的测试 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/

相关文章:

mysql - 导入 CSV 换行问题

eclipse - bash 脚本的 Aptana 语法着色

java - Android 无法执行 rm -r

bash - su输入命令中如何定义环境变量

bash - crontab 提取cron表达式

regex - 两个不同文件的 Bash grep 正则表达式问题

linux - 如何查找在过去 24 小时内修改过的文件,但不从隐藏目录中找到文件并对它们求和

python - 使用平面图时,在假设中获取 'LazyStrategy' 对象而不是整数

testing - 测试迫使Webapp抛出错误以揭示黑客攻击信息

perl - 如何从 Perl 测试文件中检查文件的内容?