2015-02-10 52 views
1

我想運行一個命令,然後檢查字符串「200 OK」的結果。如何在bash腳本中解析命令的結果

到目前爲止,我有以下bash腳本:

for i in $(seq 1 50) 
    do 
     printf "i is: $i\n" 
     RESULTS="$(curl -i -H "Accept: application/json" -X GET http://localhost/test/store/widget/123 | grep -ne "200 OK")" 
     if [ $RESULTS -eq 0 ]; then 
       echo "GET failed with $RESULTS" 
       break 
     else 
       echo "we're good" 
     fi 

done 

當我運行上面的腳本,我得到如下結果:

i is: 1 
    % Total % Received % Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 
100 85 100 85 0  0 3944  0 --:--:-- --:--:-- --:--:-- 4047 
gettest.sh: 8: [: 1:HTTP/1.1: unexpected operator 
we're good 
i is: 2 
    % Total % Received % Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 
100 85 100 85 0  0 3293  0 --:--:-- --:--:-- --:--:-- 3400 
gettest.sh: 8: [: 1:HTTP/1.1: unexpected operator 
we're good 

的輸出,我回來從運行curl命令一次看起來像這樣:

HTTP/1.1 200 OK 
Date: Tue, 10 Feb 2015 13:21:18 GMT 
Server: Apache/2.4.7 (Ubuntu) 
X-Powered-By: PHP/5.5.9-1ubuntu4.5 
Content-Length: 85 
Content-Type: application/json; charset=utf-8 

{"status":pass,"widgetinfo":"{\"widgetname\":\"the best widget\",\"price\":\"100.00\"}"} 

基於上述結果,我有幾個問題s:

  1. 爲什麼bash腳本會打印有關總時間,速度等的統計數據以獲得curl結果?
  2. 我該如何修改腳本,以便除非腳本失敗,我只想看到計數器值(i)和指示通過的狀態。如果失敗,我希望腳本退出,並詳細說明curl命令返回的內容。
  3. 爲什麼我收到錯誤

gettest.sh:8:1:HTTP/1.1:意外的運營商?

任何提示?謝謝!

回答

1

如果您不希望使用-s來打印統計數據,那麼在錯誤4xx和5xx的情況下,您可以使用-f以非零值退出。

有效的請求:

curl -sf example.com -o /dev/null ; echo $? 
0 

找不到404:

curl -sf example.com/test ; echo $? 
22 

如果你想要錯誤消息-S

curl -sfS example.com/test ; echo $? 
curl: (22) The requested URL returned error: 404 Not Found 
22 

從手冊:

man curl | grep '\-[Ssf],' -A 3 
     -f, --fail 
       (HTTP) Fail silently (no output at all) on server errors. This is mostly done to better enable scripts etc to better deal with failed 
       attempts. In normal cases when a HTTP server fails to deliver a document, it returns an HTML document stating so (which often also 
       describes why and more). This flag will prevent curl from outputting that and return error 22. 
-- 
     -s, --silent 
       Silent or quiet mode. Don't show progress meter or error messages. Makes Curl mute. 

     -S, --show-error 
       When used with -s it makes curl show an error message if it fails. 

所以,你的腳本可能是這樣的:

RESULTS="$(curl -sSfi -H "Accept: application/json" -X GET http://localhost/test/store/widget/123 -o /dev/null 2>&1)" 
if [ $RESULTS -eq 0 ]; then 
     echo "GET failed with $RESULTS" 
     break 
else 
     echo "we're good" 
fi 
0

RESULTS=$(cmd)分配命令的輸出結果,而不是由命令返回的值。如果你想檢查一個特定的字符串出現在輸出中,常見的是使用grep(這將消耗命令的輸出,所以這將是不可用):

if cmd | grep -q "some string"; then ... 

如果你想檢查結果,只是做:

if cmd; then echo cmd succeeded ... 
else echo cmd failed ... 
fi 

注意,在後一種情況下,CMD的輸出仍將被寫入腳本的標準輸出,所以你可能要重定向。

1

你可以使用此命令

curl -s -o /dev/null -w "%{http_code}" http://www.wikipedia.org 

此命令打印在標準輸出的HTTP代碼

+0

好的技巧得到HTTP_CODE用'-w' +1 – Tiago 2015-02-10 23:10:52