我想比较两个文件,看看它们在我的shell脚本中是否相同,我的方法是:
diff_output=`diff ${dest_file} ${source_file}`
if [ some_other_condition -o ${diff_output} -o some_other_condition2 ]
then
....
fi
Run Code Online (Sandbox Code Playgroud)
基本上,如果它们是相同的$ {diff_output}应该什么都不包含,上面的测试将评估为true.
但是当我运行我的脚本时,它会说
[:太多的论点
在if [....]行.
有任何想法吗?
Joh*_*ica 15
您是否关心实际差异是什么,或者文件是否不同?如果是后者,则不需要解析输出; 你可以检查退出代码.
if diff -q "$source_file" "$dest_file" > /dev/null; then
: # files are the same
else
: # files are different
fi
Run Code Online (Sandbox Code Playgroud)
或者使用cmp哪种更有效:
if cmp -s "$source_file" "$dest_file"; then
: # files are the same
else
: # files are different
fi
Run Code Online (Sandbox Code Playgroud)