使用curl和grep的Shell脚本

4

我有一个URL数组。 我想打开它们,如果它们没有任何错误地打开,则显示状态为运行,否则显示为未运行。如何通过从当前输出中删除所有其他消息来实现下面所述的期望输出。

 #!/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
2个回答

6
< p > -s 标志抑制输出:

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

来自curl手册:

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

1

添加 -S (大写) 选项以便您仍然能够看到错误:

来自 man 手册: -S/--show-error 显示错误。使用 -s,使 curl 在发生错误时显示错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接