简体   繁体   中英

Shell script : How if condition evaluates true or false

What environment variable or something internally the ' if ' keyword checks to decide true/false. I am having something like below two statements. abc is mounted , but not pqr .

if mount |grep -q "abc"; then echo "export pqr"; fi
if mount |grep -q "pqr"; then echo "export abc"; fi

In the above case I expected first statement to do nothing since abc is mounted(so finds the row in mount o/p) hence the $? after mount |grep -q "abc" is 0. And I expected second statement to execute the echo. But it's happening otherwise, first statement is printing but second not. So want to understand on what basis if decides true/false. Here is one related question

But the accepted answer for that question says :

if [ 0 ]
is equivalent to

if [ 1 ]

If this is true then both my statements should do echo, right?

There is a basic difference between the commands that you are issuing and the analogy that you are drawing from the referenced question.

When grep is executed with the -q option, it exits with a return code of zero is the match is found. This implies that if the output of mount were to contain abc , then

if mount |grep -q "abc"; then echo "export pqr"; fi

is equivalent to saying:

if true; then echo "export pqr"; fi

Note that there is no test command, ie [ , that comes into the picture here.


Quoting from the manual :

The test and [ builtins evaluate conditional expressions using a set of rules based on the number of arguments.

0 arguments

 The expression is false. 

1 argument

 The expression is true if and only if the argument is not null. 

This explains why [ 0 ] and [ 1 ] both evaluate to true.

The if command does not act, like C-like languages, on the "boolean value" of an integer: it acts on the exit status of the command that follows. In shell, an exit status of 0 is considered to be success, any other exit status is failure. If the command following if exits with status 0, that is "true"

Example:

$ test -f /etc/passwd; echo $?
0
$ test -f /etc/doesnotexist; echo $?
1
$ if test -f /etc/passwd; then echo exists; else echo does not exist; fi
exists
$ if test -f /etc/doesnotexist; then echo exists; else echo does not exist; fi
does not exist

Note that [ and [[ are (basically) commands that (basically) alias test

if [ 0 ]

tests to see if the string 0 is non-empty. It is non-empty, so the test succeeds. Similarly, if [ 1 ] succeeds because the string 1 is non-empty. The [ command (also named test ) is returning a value based on its arguments. Similarly, grep returns a value.
The if keyword causes the shell to execute commands based on the value returned by the command, but the output of the command preceded by if is irrelevant.

The command test 0 (equivalent to the command [ 0 ] returns a value of 0. The command test 1 also returns a value of 0. Zero is treated by the shell as a success, so the commands of the if clause are executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM