linux - Shell脚本中Diff的解析结果

标签 linux bash shell

我想在我的 shell 脚本中比较两个文件,看看它们是否相同,我的方法是:

diff_output=`diff ${dest_file} ${source_file}`

if [ some_other_condition -o ${diff_output} -o some_other_condition2 ]
then
....
fi

基本上,如果它们相同,${diff_output} 应该不包含任何内容,上面的测试将评估为真。

但是当我运行我的脚本时,它说
[: 参数过多

在 if [....] 行。

有什么想法吗?

最佳答案

您关心实际差异是什么,还是只关心文件是否不同?如果是后者,则无需解析输出;您可以改为检查退出代码。

if diff -q "$source_file" "$dest_file" > /dev/null; then
    : # files are the same
else
    : # files are different
fi

或者使用效率更高的cmp:

if cmp -s "$source_file" "$dest_file"; then
    : # files are the same
else
    : # files are different
fi

关于linux - Shell脚本中Diff的解析结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2671300/

相关文章:

PHP Word 导出

python - Linux 错误 : sh: qsub: command not found

linux - 如何在 Linux 中只提取文件的 inode 编号?

bash - 什么意思#!在 ${variable#!} 中? ( bash )

macos - OS X/Linux : pipe into two processes?

linux - Scheme 中的二次公式

linux - Robo3t crashing in ubuntu 16.04 and above version (This application failed start because it could not find or load the Qt platform plugin "xcb")

linux - Bash 错误报告

json - 如何使用shell脚本从json字符串中grep特定字段值

PHP 远程 Sudo 脚本执行