Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two files in makefile

I need help with comparing two files in makefile. I need something like this:

if [cmp $(FILE1) $(FILE2)] !=0; than 
 echo "OK" 
else 
 echo "WRONG" 
fi

But I am not sure how exactly to do that, Thanks

like image 950
Libor Zapletal Avatar asked Feb 16 '26 11:02

Libor Zapletal


2 Answers

Edit: corrected mistaken use of -z to -eq 0 and added Makefile context help.

It's really a shell question, not specific to makefiles, but this code would work:

cmp -s $(FILE1) $(FILE2)
RETVAL=$?
if [ $RETVAL -eq 0 ]; then 
    echo "SAME" 
else 
    echo "NOT SAME" 
fi

In a makefile rule, that would look like:

my_compare:
    cmp -s $(FILE1) $(FILE2); \
    RETVAL=$$?; \
    if [ $$RETVAL -eq 0 ]; then \
            echo "SAME"; \
    else \
            echo "NOT SAME"; \
    fi
like image 137
e.dan Avatar answered Feb 20 '26 06:02

e.dan


Return code from diff command will be 0 if files are identical, and 1 if files differs.

like image 31
Charles Brunet Avatar answered Feb 20 '26 06:02

Charles Brunet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!