我想在我的 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/