bash 脚本中比较运算符设置的退出状态
Exit status set by comparison operator in bash script
以下 bash 脚本打印 "ERROR!" 而不是 "Server error response" 即使 wget returns 8:
#!/bin/bash
wget -q "www.google.com/unknown.html"
if [ $? -eq 0 ]
then
echo "Fetch successful!"
elif [ $? -eq 8 ]
then
echo "Server error response"
else
echo "ERROR!"
fi
当上面的脚本是运行带-x的时候,第一次和0比较好像是设置退出状态为1:
+ wget www.google.com/unknown.html
+ '[' 8 -eq 0 ']'
+ '[' 1 -eq 8 ']'
+ echo 'ERROR!'
ERROR!
我通过使用一个变量来存储 wget 退出状态来解决这个问题,但是我找不到任何关于 $?已设置。 Bash 详情:
$ bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
有人可以指点我吗?
$?
在 man bash
:
的 特殊参数 参数部分进行了解释,虽然非常简短
? Expands to the exit status of the most recently executed fore-
ground pipeline.
@chepner 在他的评论中说得最好:
The key thing to understand is that each [ ... ]
is a separate foreground pipeline, not part of the if statement's syntax, and they are executed in order, updating $?
as you go along.
如果要使用 if-else 链,则将 $?
的值保存在一个变量中,并对该变量使用条件:
wget -q "www.google.com/unknown.html"
x=$?
if [ $x -eq 0 ]
then
echo "Fetch successful!"
elif [ $x -eq 8 ]
then
echo "Server error response"
else
echo "ERROR!"
fi
但在这个例子中,case
会更实用:
wget -q "www.google.com/unknown.html"
case $? in
0)
echo "Fetch successful!" ;;
8)
echo "Server error response" ;;
*)
echo "ERROR!"
esac
尝试在 $? 上使用 switch case或存储 $?在变量中。
以下 bash 脚本打印 "ERROR!" 而不是 "Server error response" 即使 wget returns 8:
#!/bin/bash
wget -q "www.google.com/unknown.html"
if [ $? -eq 0 ]
then
echo "Fetch successful!"
elif [ $? -eq 8 ]
then
echo "Server error response"
else
echo "ERROR!"
fi
当上面的脚本是运行带-x的时候,第一次和0比较好像是设置退出状态为1:
+ wget www.google.com/unknown.html
+ '[' 8 -eq 0 ']'
+ '[' 1 -eq 8 ']'
+ echo 'ERROR!'
ERROR!
我通过使用一个变量来存储 wget 退出状态来解决这个问题,但是我找不到任何关于 $?已设置。 Bash 详情:
$ bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
有人可以指点我吗?
$?
在 man bash
:
? Expands to the exit status of the most recently executed fore- ground pipeline.
@chepner 在他的评论中说得最好:
The key thing to understand is that each
[ ... ]
is a separate foreground pipeline, not part of the if statement's syntax, and they are executed in order, updating$?
as you go along.
如果要使用 if-else 链,则将 $?
的值保存在一个变量中,并对该变量使用条件:
wget -q "www.google.com/unknown.html"
x=$?
if [ $x -eq 0 ]
then
echo "Fetch successful!"
elif [ $x -eq 8 ]
then
echo "Server error response"
else
echo "ERROR!"
fi
但在这个例子中,case
会更实用:
wget -q "www.google.com/unknown.html"
case $? in
0)
echo "Fetch successful!" ;;
8)
echo "Server error response" ;;
*)
echo "ERROR!"
esac
尝试在 $? 上使用 switch case或存储 $?在变量中。