Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash scripting know the result of a command

I am writing a bash script to run an integration test of a tool I am writing.

Basically I run the application with a set of inputs and compare the results with expected values using the diff command line tool.

It's working, but I would like to enhance it by knowing the result of the diff command and print "SUCCESS" or "FAIL" depending on the result of the diff.

How can I do it?

like image 539
Tiago Veloso Avatar asked Jan 23 '26 04:01

Tiago Veloso


1 Answers

if diff file1 file2; then
    echo Success
else
    echo Fail
fi

If both files are equal, diff returns 0, which is the return code for if to follow then. If file1 and file2 differ, diff returns 1, which makes if jump to the else part of the construct.

You might want to suppress the output of diff by writing diff file1 file2 >/dev/null instead of the above.

like image 119
exic Avatar answered Jan 24 '26 20:01

exic