Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In Bash, I would like an if statement which is based of the exit code of running a command. For example:

#!/bin/bash

if [ ./success.sh ]; then
    echo "First: success!"
else
    echo "First: failure!"
fi

if [ ./failure.sh ]; then
    echo "Second: success!"
else
    echo "Second: failure!"
fi

success.sh

#!/bin/bash

exit 0

failure.sh

#!/bin/bash

exit 1

This should print out:

First: success!
Second: failure!

How would I achieve this? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
915 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just remove the brackets:

#!/bin/bash

if ./success.sh; then
    echo "First: success!"
else
    echo "First: failure!"
fi

if ./failure.sh; then
    echo "Second: success!"
else
    echo "Second: failure!"
fi

Explanation: the thing that goes between if and then is a command (or series of commands), the exit status of which is used to determine whether to run the then clause, or the else clause. This is exactly what you want.

So why do people use brackets in if statements? It's because normally you want to decide which branch of the if to run based on some conditional expression (is "$a" equal to "$b", does a certain file exist, etc). [ is actually a command which parses its arguments as a conditional expression (ignoring the final ]), and then exits with either success or failure depending on whether the conditional is true or false. Essentially, [ ] functions as an adapter that lets you use conditional expressions instead of command success/failure in your if statements. In your case, you want success/failure not a conditional expression, so don't use the adapter.

BTW, you'll also sometimes see if [[ some expression ]]; then and if (( some expression )); then. [[ ]] and (( )) are conditional expressions built into bash syntax (unlike [, which is a command). [[ ]] is essentially a better version of [ ] (with some syntax oddities cleaned up and some features added), and (( )) is a somewhat similar construct that does arithmetic expressions.

BTW2 another thing you'll see in scripts is the exit status being tested by checking the special parameter $?, which gives the exit status of the last command. It looks like this:

somecommand
if [ $? -eq 0 ]; then
    echo "Somecommand: success!"
else
    echo "Somecommand: failure!"
fi

I really consider this cargo cult programming. People are used to seeing [ ] conditional expressions in if statements, and this idiom puts the success test in the form of a conditional expression. But let me run through how it works: it takes the exit status of the command, puts it in a conditional expression, has [ ] evaluate that and turn it right back into an exit status so if can use it. That whole rigamarole is unnecessary; just put the command directly in the if statement.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...