首页 > 解决方案 > 计算文件夹中的文件列表,除了 bash 中的某些文件

问题描述

我有一个 bash 脚本针对文件夹中的一堆测试文件运行可执行文件。有些测试文件应该通过,有些应该失败。我想要做的是列出应该失败的文件,然后将文件夹中的其余测试视为阳性测试。然后,我可以分别遍历每个列表并相应地处理结果。

这就是我所拥有的,当只有 1 次失败测试时有效,但不适用于更多:

negative_tests() {
    echo "../testcases/test3.txt"
    echo "../testcases/test4.txt"
}
negative_tests=$(negative_tests)

positive_tests=$(comm -23 <(ls ../testcases/*) <(negative_tests))

log "Running tests.."
for testfile in $positive_tests; do
    ./a.out $testfile >> output.txt || { echo "Failed on $testfile." ; exit 1; }
done

for testfile in $negative_tests; do
    ./a.out $testfile >> output.txt && { echo "Succeeded on $testfile succeeded when failure was expected."; exit 1; }
done

我有一种感觉,我只是错过了 bash 数据模型的工作原理。有什么想法或更好的方法吗?

标签: bash

解决方案


看起来这种方法有效,但有效的方式comm是它期望它区分的行是有序的。因此,我需要将否定案例导入sort,以便它们与以下文件的顺序相匹配ls

positive_tests=$(comm -23 <(ls ../testcases/*) <(negative_tests | sort))

然后,一切都按预期工作。虽然我有兴趣看到人们可能拥有的任何其他解决方案。


推荐阅读