下面的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