基于命令退出代码的Bash条件 [英] Bash conditional based on exit code of command

查看:101
本文介绍了基于命令退出代码的Bash条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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!

我将如何实现?谢谢!

推荐答案

只需移除括号:

#!/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

说明:ifthen之间的是命令(或一系列命令),其退出状态用于确定是运行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语句中使用方括号?这是因为通常您要根据某些条件表达式("$a"等于"$b",是否存在某个文件等)来决定要运行if的哪个分支. [实际上是一个命令,它将其参数解析为条件表达式(忽略最终的]),然后根据条件是true还是false退出,成功或失败.本质上,[ ]充当适配器,使您可以在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 ]]; thenif (( some expression )); then. [[ ]](( ))是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您将在脚本中看到的另一件事是通过检查特殊参数$?来测试退出状态,该参数给出了最后一条命令的退出状态.看起来像这样:

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语句中看到[ ]条件表达式,这个习惯用法将成功测试以条件表达式的形式进行.但是让我来看一下它是如何工作的:它获取命令的退出状态,将其放入条件表达式中,让[ ]评估该并将其立即返回退出状态,因此可以使用它.整个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆