bash - 计算文件夹中的文件列表,除了 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 数据模型的工作原理。有什么想法或更好的方法吗?
解决方案
看起来这种方法有效,但有效的方式comm
是它期望它区分的行是有序的。因此,我需要将否定案例导入sort
,以便它们与以下文件的顺序相匹配ls
:
positive_tests=$(comm -23 <(ls ../testcases/*) <(negative_tests | sort))
然后,一切都按预期工作。虽然我有兴趣看到人们可能拥有的任何其他解决方案。
推荐阅读
- elixir - 有没有办法让 GenServer 状态持久化?
- reactjs - ESLint 在 React 的新语法中返回语法错误
- ruby-on-rails - 为什么我的 Ruby 函数网页只抓取第一页而不抓取分页页面?
- docker - 将 F# 4.5 安装到 Docker 容器
- javascript - 如何定位正在窃取 DOM 焦点的函数?
- python - RegEx 在 (Number-Number)、(Number-char)、(Char-Special char) 和 (special char- char) 之间插入空格,除了 '\'
- ruby-on-rails - 在服务器之间迁移会破坏旧的操作文本附件,新附件可以工作
- google-calendar-api - 日历循环规则 - 连续天:每个第一个和第三个星期五和星期六
- android - 使用 ARCore 的示例点云
- vba - 注销 Windows Server 2016 时触发规则时 Outlook vba 打开代码窗口