Return value processing of Linux diff

Hao, you Jianqiang 2022-06-24 07:56:43 阅读数:163


diff You can check whether two files are different contents , His results can be controlled by parameters

Here we mainly introduce four parameters

-a or –text diff By default, text files are only compared line by line .
-b or –ignore-space-change Don't check for differences in space characters .
-B or –ignore-blank-lines Don't check blank lines .
-q or –brief Just show if there is a difference , Don't show detailed information .

diff -a -b -B -q a.txt b.txt > out.log

This result will be saved to out.log In the document
With different results , The return value will be different , Can pass $? To see what the return value is , If diff Different files will return greater than 0 Parameters of , That is to say exit 1 exit 2 wait , This will cause our program to exit , If we want to deal with it ourselves diff Result , It can be handled as follows

diff -a -b -B -q a.txt b.txt > out.log || true

This ensures diff The result is always true

copyright:author[Hao, you Jianqiang],Please bring the original link to reprint, thank you.