解析DIFF的结果Shell脚本 [英] Parsing result of Diff in Shell Script

查看:922
本文介绍了解析DIFF的结果Shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要比较两个文件,​​看看他们都在我的shell脚本相同与否,我的办法是:

I want to compare two files and see if they are the same or not in my shell script, my way is:

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

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

基本上,如果它们是相同的$ {} diff_output应该包含什么和上面的测试将评估为true。

Basically, if they are the same ${diff_output} should contain nothing and the above test would evaluate to true.

但是当我运行我的脚本,它说结果
[:参数太多

But when I run my script, it says
[: too many arguments

在如果[...]行。

任何想法?

推荐答案

你关心什么实际的区别,或者只是文件是否不同?如果是后者不需要解析输出;您可以检查出口code来代替。

Do you care about what the actual differences are, or just whether the files are different? If it's the latter you don't need to parse the output; you can check the exit code instead.

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

这篇关于解析DIFF的结果Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆