2013-05-30 79 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时,发生卷曲显示错误时