Shell script with curl and grep

I have a url array. I want to open them, and if they open without any errors, show the status, because the work has not yet been started. How can I get the desired result mentioned below by deleting all other messages from the current output.

#!/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 

Current script output

  % 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 

Desired conclusion:

 Running Not Running Not Running 
+4
source share
2 answers

The -s flag suppresses output:

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

On the curl man page:

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

add -S (top) so you can still see errors:

from person: -S / - show-error Show error. If -s, make curl show errors when they occur

+1
source

Source: https://habr.com/ru/post/1483646/


All Articles