如果 diff 命令导致 bash 没有差异,如何输出“通过”?

jos*_*non 1 c testing shell diff automated-tests

我正在编写一个循环遍历我的 ./tests 目录的 shell 脚本,并使用 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
Run Code Online (Sandbox Code Playgroud)

我的问题是如何向脚本添加 if 语句,如果 diff 命令导致没有区别,则该语句将输出“passed”?例如

伪代码:

if ((diff res_file out_file) == '') {
    echo 'Passed'
} else {
    printf "Failed\n\n"
    diff res_file out_file
}
Run Code Online (Sandbox Code Playgroud)

Jst*_*lls 6

从 diff 命令获取并检查退出代码。如果没有发现差异,diff 的退出代码为 0。

diff ...
ret=$?

if [[ $ret -eq 0 ]]; then
    echo "passed."
else
    echo "failed."
fi
Run Code Online (Sandbox Code Playgroud)