How to compare two files in shell script?

喜夏-厌秋 提交于 2019-11-29 12:56:21

I think what you need is :

if [ "$d" != NULL ];

Try.

Unless you are writing a script for portability to the original Bourne shell or others that do not support the feature, in Bash and ksh you should use the [[ form of test for strings and files.

There is a reduced need for quoting and escaping, additional conditions such as pattern and regular expression matching and the ability to use && and || instead of -a and -o.

if [[ $var == $var1 ]]

Also, "NULL" is not a special value in Bash and ksh and so your test will always succeed since $d is tested against the literal string "NULL".

if [[ $d != "" ]]

or

if [[ $d ]]

For numeric values (not including leading zeros unless you're using octal), you can use numeric expressions. You can omit the dollar sign for variables in this context.

numval=41
if ((++numval >= 42))  # increment then test
then
    echo "don't panic"
fi

It's not necessary to use echo and cut for substrings. In Bash and ksh you can do:

var=${line:3:23}

Note: cut uses character positions for the beginning and end of a range, while this shell construct uses starting position and character count so you have to adjust the numbers accordingly.

And it's a good idea to get away from using backticks. Use $() instead. This can be nested and quoting and escaping is reduced or easier.

I think you could use the DIFF command

diff file1 file2 > whats_the_diff.txt
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!