下面的bash脚本输出“ERROR!”而不是“服务器错误响应”,即使wget的回报8:由bash脚本中的比较运算符设置的退出状态
#!/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 --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 wget |更少+/EXIT'? – Cyrus