Take a look at two snippets of code first.
[[ ${code} -ne 0 ]] && fatal "${msg}"
and
[[ ${code} -eq 0 ]] || fatal "${msg}"
Apart from the use of short circuit AND and short circuit OR, they do pretty much the same thing, that compares the value of code and if it does not equal to 0 calls the fatal function with the value of msg.
But if this is the last line in a function, it will affect the return code of the function. Let's say the value of code indeed is 0, the first line would return a non-zero value, because the equality comparison would fail. Being equal to the conventional success value, code usually means whatever before this line succeeds. The first usage would, however, result in the function returning a failure value, which would hardly be what we want.
We could of course add something like exit 0 after this line. But being a minimalist as I am, you'd want to keep your code as simple as possible. Then use the second form. Simple as that.
没有评论:
发表评论