Learner
Learner

Reputation: 1968

Use of || after > /dev/null 2>&1 || {exit 1}

I am new to bash scripting and I am trying to understand the following line:

# Verify pre-req environment
command -v kubectl > /dev/null 2>&1 || { echo "kubectl pre-req is missing."; exit 1; }

I unerstand this part: command -v kubectl > /dev/null 2>&1 so if it fails or not we redirect the output to /dev/null in which if I understand it correctly means ignore it in all cases. So in what cases the following line will be executed then?

{ echo "kubectl pre-req is missing."; exit 1; }

and how || behaves in this case?

Upvotes: 0

Views: 2240

Answers (4)

Gonzalo Matheu
Gonzalo Matheu

Reputation: 10064

|| is a logic or and acts based on previous command exit code (0 means true and other value is false).

In this case, it will only execute the echo part when the first command (kubectl..) exit code is not 0 (i.e: fails). The exit 1 is to make the whole command fail, otherwise the exit code would be 0 from echo command.

Upvotes: 1

Hansraj Das
Hansraj Das

Reputation: 239

|| is used for OR condition in bash.

For a simplified example lets look at

 cmd1 || cmd2

What happens is that if cmd1 succeeds (i.e. exit code 0), then cmd2 will NOT be performed. cmd2 will only be performed if cmd1 fails.

This is one reason why returning exit codes is so important.

For more details, please look at this post: Boolean operators ( &&, -a, ||, -o ) in Bash

Upvotes: 2

ruakh
ruakh

Reputation: 183321

I unerstand this part: command -v kubectl > /dev/null 2>&1 so if it fails or not we redirect the output to /dev/null in which if I understand it correctly means ignore it in all cases.

You've misunderstood that; no part of command -v kubectl > /dev/null 2>&1 has any connection to "if it fails" vs. "not".

Rather, > and 2> redirect two different types of output; > (or 1>) redirects whatever the command writes to standard output, and 2> redirects whatever the command writes to standard error. Whether the command writes anything to standard output is independent of whether it ends up succeeding or failing; whether it writes anything to standard error is independent of whether it ends up succeeding or failing; and whether it writes anything to standard output is independent of whether it writes anything to standard error.

For example, here is a command that prints to both standard output and standard error, and then returns successfully:

( echo "this is on standard output" ; echo "this is on standard error" >&2 ; exit 0 )

and here is one that prints to both standard output and standard error, and then fails:

( echo "this is on standard output" ; echo "this is on standard error" >&2 ; exit 1 )

By contrast, || really does have to do with whether a command succeeds (exiting with zero) or fails (exiting with something other than zero).

Upvotes: 2

glenn jackman
glenn jackman

Reputation: 246827

If the kubectl command exits with a non-zero exit status, the code after the || will be executed.

$ sh -c 'exit 42' && echo "exit status zero" || echo "exit status non-zero"
exit status non-zero

$ sh -c 'exit 0' && echo "exit status zero" || echo "exit status non-zero"
exit status zero

Upvotes: 1

Related Questions