gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:47:00 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com