基于命令退出代码的 Bash 条件 [英] Bash conditional based on exit code of command
问题描述
在 Bash 中,我想要一个基于运行命令的退出代码的 if 语句.例如:
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
这应该打印出来:
First: success!
Second: failure!
我将如何实现这一目标?谢谢!
How would I achieve this? Thanks!
推荐答案
去掉括号:
#!/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
说明:if
和then
之间的东西是一个命令(或一系列命令),它的退出状态用于决定是否运行then
子句,或 else
子句.这正是您想要的.
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.
那么为什么人们在 if
语句中使用方括号?这是因为通常你想根据一些条件表达式来决定运行 if
的哪个分支("$a"
等于 "$b"代码>,是否存在某个文件等).
[
实际上是一个命令,它将其参数解析为条件表达式(忽略最后的 ]
),然后根据条件是否为真或以成功或失败退出错误的.从本质上讲,[ ]
用作适配器,允许您在 if
语句中使用条件表达式而不是命令成功/失败.在您的情况下,您希望成功/失败不是条件表达式,所以不要使用适配器.
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.
顺便说一句,你有时也会看到 if [[ some expression ]];then
and if (( some expression ));然后
.[[ ]]
和 (( ))
是内置于 bash 语法中的条件表达式(与 [
不同,它是一个命令).[[ ]]
本质上是 [ ]
的更好版本(清理了一些语法上的奇怪之处并添加了一些功能)和 (( ))
是一个有点类似的构造,用于算术表达式.
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
我真的考虑过这种货物崇拜编程.人们习惯于在if
语句中看到[ ]
条件表达式,而这个习语将成功测试放在条件表达式的形式中.但是让我来看看它是如何工作的:它获取命令的退出状态,将其放入条件表达式中,让 [ ]
评估它并将其转回退出状态 所以 if
可以使用它.整个rigamarole是不必要的;只需将命令直接放在if
语句中即可.
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.
这篇关于基于命令退出代码的 Bash 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!