我正在尝试编写一段脚本,以测试我的文件是否完全符合要求,但我以前没有使用过bash:
#!/bin/bash
./myfile <test.in 1>>test.out 2>>testerror.out
if cmp -s "test.out" "pattern.out"
then
echo "Test matches pattern"
else
echo "Test does not match pattern"
fi
if cmp -s "testerror.out" "pattern.err"
then
echo "Errors matches pattern"
else
echo "Errors does not match pattern"
fi
我能否这样编写代码,执行"./script.sh myfile pattern" 后程序会运行在所有名为pattern*.in的文件上,并检查myfile是否生成了与pattern*.out和pattern*.err文件相同的结果?例如,有文件pattern1、pattern2、pattern4需要测试,但没有pattern3。
如果我不需要创建新文件,我是否可以绕过这一步骤?(假设我并不需要它们)如果我从命令行操作,我会采取类似以下的方式:
< pattern.in ./myfile | diff -s ./pattern.out
但我不知道如何在脚本文件中编写它,以使其正常工作。
或者也许我应该每次都使用rm命令?
for
循环遍历与模式匹配的所有文件。 - Barmarfor file in pattern*.in
- Barmar< "$file" ./myfile
- Barmarmyfile
和pattern
作为命令行参数提供给script.sh
吗? - cxw