Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shell script with curl and grep

Tags:

curl

I have an array of url's. I want to open them and if they are opening up without any error , show the status as running else not running. How can I achieve the desired output mentioned below, by removing all other messages from 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 output of script is

  % 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 output:

Running
Not Running
Not Running
like image 287
Jill448 Avatar asked Mar 24 '23 18:03

Jill448


1 Answers

The -s flag suppresses output:

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

From the curl man page:

   -s/--silent
          Silent or quiet mode. Don't show progress meter or error messages.  Makes Curl mute.
like image 99
Kevin Avatar answered Apr 02 '23 06:04

Kevin