如果 diff 命令导致 bash 没有差异,如何输出 'passed'?

How to output 'passed' if a diff command results in no difference in bash?

我正在编写一个 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 命令结果没有差异,我如何向将输出 'passed' 的脚本添加 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

@jstills 的回答对我有用,但是我稍微修改了它并认为我 post 我的结果也可以作为帮助其他人的答案

一旦我了解到 diff 的退出代码为 0,我就修改了我的代码。如果我理解正确,它会检查 diff 是否以 0 或 >1 退出每个差异。然后我的代码将 diff 的输出发送到 /dev/null 所以它不会显示到 stdout 然后我进行检查并打印通过或失败到 stdout 如果失败则与 sdiff 的差异并排显示差异.

if diff "${t%.*}.res" "${t%.*}.out" >/dev/null; then
    printf "Passed\n"
else
    printf "Failed\n"
    sdiff "${t%.*}.res" "${t%.*}.out"
fi