bash Shell Script中Diff的解析结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2671300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing result of Diff in Shell Script
提问by Saobi
I want to compare two files and see if they are the same or not in my shell script, my way is:
我想比较两个文件,看看它们在我的 shell 脚本中是否相同,我的方法是:
diff_output=`diff ${dest_file} ${source_file}`
if [ some_other_condition -o ${diff_output} -o some_other_condition2 ]
then
....
fi
Basically, if they are the same ${diff_output} should contain nothing and the above test would evaluate to true.
基本上,如果它们相同,则 ${diff_output} 应该不包含任何内容,并且上述测试将评估为真。
But when I run my script, it says
[: too many arguments
但是当我运行我的脚本时,它说
[: 参数太多
On the if [....] line.
在 if [....] 行。
Any ideas?
有任何想法吗?
回答by John Kugelman
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
Or use cmpwhich is more efficient:
或者使用cmp哪个更有效:
if cmp -s "$source_file" "$dest_file"; then
: # files are the same
else
: # files are different
fi
回答by Cascabel
There's an option provided precisely for doing this: -q(or --quiet). It tells diff to just let the exit code indicate whether the files were identical. That way you can do this:
为此提供了一个选项:(-q或--quiet)。它告诉 diff 只让退出代码指示文件是否相同。这样你就可以这样做:
if diff -q "$dest_file" "$source_file"; then
# files are identical
else
# files differ
fi
or if you want to swap the two clauses:
或者如果你想交换这两个子句:
if ! diff -q "$dest_file" "$source_file"; then
# files differ
else
# files are identical
fi
If you really wanted to do it your way (i.e. you need the output) you should do this:
如果你真的想按照你的方式做(即你需要输出),你应该这样做:
if [ -n "$diff_output" -o ... ]; then
...
fi
-nmeans "test if the following string is non-empty. You also have to surround it with quotes, so that if it's empty, the test still has a string there - you're getting your error because your test evaluates to some_other_condition -o -o some_other_condition2, which as you can see isn't going to work.
-n表示“测试以下字符串是否为非空。您还必须用引号将其括起来,以便如果它为空,则测试在那里仍然有一个字符串 - 您会收到错误,因为您的测试评估为some_other_condition -o -o some_other_condition2,正如您可以看到是行不通的。
回答by Jürgen H?tzel
diffis for comparing files line by linefor processing the differences tool like patch. If you just want to check if they are different, you should use cmp:
diff用于逐行比较文件以处理诸如patch 之类的差异工具。如果您只想检查它们是否不同,您应该使用cmp:
cmp --quiet $source_file $dest_file || echo Different
回答by arun
diff $FILE $FILE2
if [ $? = 0 ]; then
echo “TWO FILES ARE SAME”
else
echo “TWO FILES ARE SOMEWHAT DIFFERENT”
fi
回答by Raman Kathpalia
Check files for difference in bash
Check files for difference in bash
source_file=abc.txt
dest_file=xyz.txt
if [[ -z $(sdiff -s $dest_file $source_file) ]]; then
echo "Files are same"
else
echo "Files are different"
fi

