我对使用 bash 相当陌生,并试图创建一个自动评分器脚本来运行一些测试用例。目前我的 bash 脚本似乎表现得很奇怪;当我设置了 -e 标志时,当 diff 的大小为正时,bash 将退出,而当未设置 -e 标志时,脚本会忽略 diff 文件中的任何差异,并表示所有测试都通过了。
脚本在“write_diff_out=....”命令后立即退出,不打印下一行。我只包含了脚本的差异部分,因为其他一切都运行良好(文件也都存在)。
# Validate outputs and print results
echo "> Comparing current build's final memory output with golden memory output...";
for file in `ls test_progs`;
do
file=$(echo $file | cut -d '.' -f1);
echo "$file";
write_diff_out=$(diff ./log/$file.writeback.out ./log/$file.writeback.gold.out > ./diff/$file.writeback.diff);
echo "Finished write_diff";
program_diff_out=$(diff -u <(grep -E '@@@' ./log/$file.program.out) <(grep -E '@@@' ./log/$file.program.gold.out) > ./diff/$file.program.diff);
echo "Finished program diff";
if [ -z "$write_diff_out" ] && [ -z "$program_diff_out" ]; then
printf "%20s:\e[0;32mPASSED\e[0m\n" "$file";
else
printf "%20s:\e[0;31mFAILED\e[0m\n" "$file";
fi
done
echo "> Done comparing test outputs.";
随意提出一种更好的格式化 diff 命令的方法,我知道有不同的方法来编写它们。