bash 如何使用 if 语句检查退出状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26675681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check the exit status using an if statement
提问by deadcell4
I was wondering what would be the best way to check the exit status in an if statement in order to echo a specific output.
我想知道在 if 语句中检查退出状态以回显特定输出的最佳方法是什么。
I'm thinking of it being
我在想它是
if [ $? -eq 1 ]
then
echo "blah blah blah"
fi
The issue I am also having is that the exit statement is before the if statement simply because it has to have that exit code. Also, I know I'm doing something wrong since the exit would obviously exit the program.
我也遇到的问题是 exit 语句在 if 语句之前,仅仅是因为它必须具有退出代码。另外,我知道我做错了什么,因为退出显然会退出程序。
回答by Etan Reisner
Everycommand that runs has an exit status.
运行的每个命令都有一个退出状态。
That check is looking at the exit status of the command that finished most recently before that line runs.
该检查正在查看在该行运行之前最近完成的命令的退出状态。
If you want yourscript to exit when that test returns true (the previous command failed) then you put exit 1
(or whatever) inside that if
block after the echo
.
如果你想你的脚本退出时测试返回true(以前的命令失败),那么你把exit 1
(或其他)里面if
块之后echo
。
That being said if you are running the command and wanting to test its output using the following is often more straight-forward.
话虽如此,如果您正在运行该命令并希望使用以下命令测试其输出通常更直接。
if some_command; then
echo command returned true
else
echo command returned some error
fi
Or to turn that around use !
for negation
或者把它转过来!
用于否定
if ! some_command; then
echo command returned some error
else
echo command returned true
fi
Note though that neither of those cares whatthe error code is. If you know you only care about a specific error code then you need to check $?
manually.
但请注意,这些人都不关心错误代码是什么。如果您知道您只关心特定的错误代码,那么您需要$?
手动检查。
回答by Oo.oO
Note that exit codes != 0 are used to report error. So, it's better to do:
请注意,退出代码 != 0 用于报告错误。所以,最好这样做:
retVal=$?
if [ $retVal -ne 0 ]; then
echo "Error"
fi
exit $retVal
instead of
代替
# will fail for error codes > 1
retVal=$?
if [ $retVal -eq 1 ]; then
echo "Error"
fi
exit $retVal
回答by chepner
$?
is a parameter like any other. You can save its value to use before ultimately calling exit
.
$?
是一个像任何其他参数一样的参数。您可以在最终调用exit
.
exit_status=$?
if [ $exit_status -eq 1 ]; then
echo "blah blah blah"
fi
exit $exit_status
回答by Catskul
Alternative to explicit if
statement
显式if
语句的替代
Minimally:
最低限度:
test $? -eq 0 || echo "something bad happened"
test $? -eq 0 || echo "something bad happened"
Complete:
完全的:
EXITCODE=$?
test $EXITCODE -eq 0 && echo "something good happened" || echo "something bad happened";
exit $EXITCODE
回答by codeforester
Just to add to the helpful and detailed answer:
只是为了添加有用和详细的答案:
If you have to check the exit code explicitly, it is better to use the arithmetic operator, (( ... ))
, this way:
如果您必须明确检查退出代码,最好使用算术运算符 ,(( ... ))
这样:
run_some_command
(($? != 0)) && { printf '%s\n' "Command exited with non-zero"; exit 1; }
Or, use a case
statement:
或者,使用case
语句:
run_some_command; ec=$? # grab the exit code into a variable so that it can
# be reused later, without the fear of being overwritten
case $ec in
0) ;;
1) printf '%s\n' "Command exited with non-zero"; exit 1;;
*) do_something_else;;
esac
Related answer about error handling in Bash:
关于 Bash 中的错误处理的相关答案: