if语句中diff whist的输出?

output from diff whist in if statement?

我正在使用 diff 操作来检查几个文件,但我想知道我是否可以同时使用它来触发事件(例如 echo-ing 一行到终端等) 而不必 'manually' 检查输出报告是否存在。

我曾考虑过使用报告创建作为触发器,但据我所知,无论如何都会创建文件,如果没有差异,它只会是空的?

基本上,是否可以让 diff 输出文件,同时在 if 语句中,以保持我的 bash 配置文件和脚本本身尽可能整洁?

(如果有帮助,这是我在这个问题中收到的精彩帮助的后续问题:

例如有这种效果吗?:

if [ diff filex filey > report.txt == true ]
   # So the report.txt is created but the 'state' of
   # the diff query is preserved and evaluated...
     then echo "Files are different"
     else
         break
fi

希望这是有道理的。

这是 Ubuntu bash 仅供参考。

是的,如果没有差异,diff 会以状态 0 退出(即 shell 对应 "true"),因此您可以这样做:

if ! diff <file1> <file2>
then
  do the thing when there are diffs
else
  do the thing when all is same
fi

即使您重定向输出,这也应该有效。