Bash:有条件,基于命令的特定退出代码 [英] Bash: conditional based on specific exit code of command
问题描述
此问题是对>基于退出的Bash条件的后续操作命令代码.
我了解并已利用该问题的公认答案:
I understand, and have made use of the accepted answer to that question:
$ cat ./txt
foo
bar
baz
$ if ! grep -Fx bax txt &>/dev/null; then echo "not found"; fi
not found
$
但是我无法弄清楚从表面上看似简单的转折的语法:如何为命令的特定退出代码编写条件?
But I can't figure out the syntax to pull off a seemingly simple twist to the premise: how can I write a conditional for a specific exit code of a command?
这篇文章指出指出 grep
可能会失败,错误代码> 1;所以我想写上面的条件来打印未找到",特别是在 grep
返回错误代码1的情况下.如何编写这样的语句?以下似乎不起作用(我显然在挣扎):
This post pointed out that grep
might fail with error code > 1; so I wanted to write the above conditional to print "not found" specifically if grep
returned error code 1. How can I write such a statement? The following don't seem to work (I'm obviously flailing):
$ # Sanity-check that grep really fails with error code 1:
$ grep -Fx bax txt &>/dev/null
$ echo $?
1
$
$
$ if grep -Fx bax txt &>/dev/null -eq 1; then echo "not found"; fi
$ if grep -Fx bax txt &>/dev/null == 1; then echo "not found"; fi
$ if grep -Fx bax txt &>/dev/null = 1; then echo "not found"; fi
$ if [ grep -Fx bax txt &>/dev/null -eq 1 ]; then echo "not found"; fi
$ if [ grep -Fx bax txt &>/dev/null == 1 ]; then echo "not found"; fi
$ if [ grep -Fx bax txt &>/dev/null = 1 ]; then echo "not found"; fi
$
注意:出于特定的原因,我特别想避免先运行该命令,然后再在条件语句中使用 $?
,
推荐答案
如果您想对不同的错误代码做出不同的响应(与链接的问题中的成功/失败相反),则需要运行以下命令并然后检查 $?
以获得其特定的退出状态:
If you want to respond differently to different error codes (as opposed to just success/failure, as in the linked question), you need to run the command and then check $?
to get its specific exit status:
grep -Fx bax txt &>/dev/null
if [ $? -eq 1 ]; then
echo "not found"
fi
(在对链接问题的回答中,我描述了如何测试 $?
是否为零,这是针对货物崇拜的编程;这不适用于检查特定的非零值.)
(In my answer to the linked question, I described testing whether $?
is zero as cargo cult programming; this does not apply to checking it for specific nonzero values.)
请注意,对于每个运行的命令,都将重置 $?
-包括测试上一个命令中的 $?
的命令-因此,如果要执行此操作您必须立即将其存储在一个变量中,然后在该变量上运行测试:
Note that $?
is re-set for every command that runs -- including one that tests $?
from the previous command -- so if you want to do more than one thing with it you must immediately store it in a variable, then run your tests on that variable:
curl "$url"
curl_status=$?
if [ "$curl_status" -eq 6 ]; then
echo "Couldn't resolve host name"
elif [ "$curl_status" -eq 7 ]; then
echo "Couldn't connect to host"
elif [ "$curl_status" -ne 0 ]; then
echo "Some sort of error occurred; curl status was $curl_status"
fi
请注意,使用!
取消命令的成功/失败将有效地破坏特定的错误代码,因为它将所有非零代码转换为相同的结果:零.如果要执行 if的等效功能!命令...
并仍然可以访问特定的代码,我最喜欢的习惯用法是使用 |||
和一个大括号组,例如:
Note that using !
to negate the success/failure of a command effectively destroys the specific error code, because it converts all nonzero codes to the same result: zero. If you want to do the equivalent of if ! command ...
and still have access to the specific code, my favorite idiom is to use ||
and a brace group, like this:
curl "$url" || {
curl_status=$?
...
# Do error handling/reporting based on $curl_status
...
}
这篇关于Bash:有条件,基于命令的特定退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!