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

标签 c shell testing automated-tests diff

我正在编写一个 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/

相关文章:

c - C 程序中的陷阱命令?

ruby-on-rails - 如何测试 Rails 迁移?

c# - 如何测试发送多封邮件?

c - 如何在 C 中将 strncpy 与 for 循环一起使用?

c - 我的程序出了什么问题?

linux - 如何在 Bash/Ubuntu 12.04 中正确编写这个算术表达式?

在 Ubuntu 中自动安装 OpenJDK 和 Apache Tomcat 的 Shellcript

facebook - 如何针对不同的测试数据集自动执行 FQL 查询的集成测试?

c - 每个 cpu 架构的真正 ELF TLS ABI 要求是什么?

c - TEMP_FAILURE_RETRY 和 __USE_GNU