我正在编写一个 shell 脚本,它循环遍历我的 ./tests 目录,并使用 unix diff 命令为我的 C 程序比较 .in 和 .out 文件。这是我的 shell 脚本:
#! /usr/bin/env bash
count=0
# Loop through test files
for t in tests/*.in; do
echo '================================================================'
echo ' Test' $count
echo '================================================================'
echo 'Testing' $t '...'
# Output results to (test).res
(./snapshot < $t) > "${t%.*}.res"
# Test with diff against the (test).out files
diff "${t%.*}.res" "${t%.*}.out"
echo '================================================================'
echo ' Memcheck
echo '================================================================'
# Output results to (test).res
(valgrind ./snapshot < $t) > "${t%.*}.res"
count=$((count+1))
done
我的问题是,如果 diff 命令结果没有差异,我如何向将输出“通过”的脚本添加 if 语句?例如
伪代码:
if ((diff res_file out_file) == '') {
echo 'Passed'
} else {
printf "Failed\n\n"
diff res_file out_file
}
最佳答案
从 diff 命令获取并检查退出代码。如果未发现差异,diff 的退出代码为 0。
diff ...
ret=$?
if [[ $ret -eq 0 ]]; then
echo "passed."
else
echo "failed."
fi
关于c - 如果 diff 命令导致 bash 没有差异,如何输出 'passed'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36783459/