4

I just wanted to understand the meaning of the following statement and whether they seem correct.

test -x /usr/bin/find || exit 0 
Command 1
Command 2
Command 3

The output of test -x /usr/bin/find is always 0. That means the exit 0 command will be executed , meaning Command 1, 2, 3 will never be executed. Am I right here?

Kusalananda
  • 333,661

2 Answers2

7

test -x /usr/bin/find (or [ -x /usr/bin/find ]) does not output anything. The test will be true if /usr/bin/find is an existing executable file, and false if the pathname does not exist, or if it's not executable.

If test exits successfully (with a zero exits status, signifying "no error"), the shell will execute the rest of the commands. If it exits with a failure (a non-zero exit status, signifying "some error"), exit 0 will terminate the current shell, preventing the rest of the commands from running.

It would arguably be better to use exit 1 or just exit in place of exit 0 when find can't be found in /usr/bin though. Using exit 0 masks the exit status of test (which would be non-zero), and prevents the caller of this script from being notified of the failure of finding find at the given location.

Related to the fact that an exit status of zero evaluates to "true" when tested as a boolean in the shell:

Related to using || and && in general:

Kusalananda
  • 333,661
4

No, they will always be executed. In man bash it says (it works like that in each POSIX shell):

   An OR list has the form
      command1 || command2

command2 is executed if, and only if, command1 returns a non-zero exit status.

Since test -x /usr/bin/find returns 0 command2 (exit 0 in this case) is not executed and script continues execution.