2013-05-30 119 views
4

我有一個url數組。我想打開它們,如果它們打開時沒有任何錯誤,則顯示狀態爲正在運行,否則未運行。如何通過從當前輸出中刪除所有其他消息來實現下面提到的所需輸出。使用curl和grep的shell腳本

#!/bin/ksh 
urlArray=('http://url1:port1' 'http://url2:port2' 'http://url3:port3') 
for url in "${urlArray[@]}" 
do 
    result=`curl $url | head -1` 

    if (echo $result | grep '<?xml' >/dev/null 2>&1); then 
     echo Running 
    else 
     echo Not Running 
    fi 
done 

當前腳本的輸出是

% Total % Received % Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 
100 12980 0 12980 0  0 711k  0 --:--:-- --:--:-- --:--:--  0 
Running 

curl: (6) Couldn't resolve host 'url2:port2' 
Not Running 

curl: (6) Couldn't resolve host 'url3:port3' 
Not Running 

希望的輸出:

Running 
Not Running 
Not Running 

回答

5

-s標誌禁止輸出:

$ curl foo 
curl: (6) Couldn't resolve host 'foo' 
$ curl -s foo 
$ 

來自卷邊手冊頁:

-s/--silent 
      Silent or quiet mode. Don't show progress meter or error messages. Makes Curl mute. 
1

添加-S(上),所以你仍然可以看到錯誤:

從人:-S/- 顯示錯誤顯示錯誤。使用-s時,發生捲曲顯示錯誤時