开发者

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 resul开发者_运维百科ts 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?


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.


The $? variable holds the result of the last executed command.


Also, in Bash you can diff the outputs of commands directly using process substitution:

if diff <(some_command arg1) <(some_command arg1) > /dev/null 2>&1
then
    echo "They're the same."
else
    echo "They're different."
fi
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜