How do I get (only) the http status of a site in a shell script?

user4069

I figure curl would do the job. I wrote in a script:

#!/bin/sh

function test {
    res=`curl -I $1 | grep HTTP/1.1 | awk {'print $2'}`
    if [ $res -ne 200 ]
    then
        echo "Error $res on $1"
    fi
}  

test mysite.com
test google.com

The problem here is no matter what I do I can't get it to stop printing the below to stdout:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

I want a cronjob to run this script and if it writes such a message then every time I run it I'll get an email because something has been printed to stdout in cron, even though the site may be fine.

How do I get the status code without getting junk into stdout? This code works except the bonus junk to the stdout preventing me from using it.

ДМИТРИЙ МАЛИКОВ
   -s/--silent
          Silent or quiet mode. Don't show progress meter 
          or error messages.  Makes Curl mute.

So your res should look like

res=`curl -s -I $1 | grep HTTP/1.1 | awk {'print $2'}`

Result is Error 301 on google.com, for example.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I fix a HTTP Status 500 Index Error?

From Dev

How do I script a filecount in android shell?

From Dev

How to get the exit status set in a shell script in Python

From Dev

How do I write a shell script to get a given process idle time?

From Dev

How do I use a regex in a shell script?

From Dev

How do I return a HTTP 404 status code from a SPA?

From Dev

Why do I get `Only one connection receive subscriber allowed` when the response has an HTTP error status code?

From Dev

How do I get only the extension of a file when writing a shell command for a context menu

From Dev

How do I close stdin in a shell script?

From Dev

How do I script a filecount in android shell?

From Dev

Bash shell script: how do I exit and restart the script?

From Dev

How do I get an HTTP status code from TTransport exceptions?

From Dev

How do I get my shell script to automatically accept prompts?

From Dev

How do I wait for a file in the shell script?

From Dev

How to split text and get only array value by using shell script?

From Dev

MVC Site getting 500 error only on server, how do I get more info on error?

From Dev

How do I write a shell script to get a given process idle time?

From Dev

How do I handle switches in a shell script?

From Dev

How do I get an acceptable last modified timestamp for my Jekyll site using a shell script?

From Dev

How do I get to the shell on ubuntu tablet?

From Dev

How do I write shell script for Redhat Linux bash shell

From Dev

How do I log into bash shell only?

From Dev

How do I get screen resolution in a shell script on Linux

From Dev

Why do I get different exit status for ps | grep in a script?

From Dev

How do i run a shell script, in a shell script with the menu dialog?

From Dev

How do I get XDG_CONFIG_HOME from a shell script / command line?

From Dev

How do you set the exit status of a shell script?

From Dev

How do I get the 'application_uris' From a Bluemix Cloudfoundry App in shell script?

From Dev

How do I add a line to the shell script?

Related Related

  1. 1

    How do I fix a HTTP Status 500 Index Error?

  2. 2

    How do I script a filecount in android shell?

  3. 3

    How to get the exit status set in a shell script in Python

  4. 4

    How do I write a shell script to get a given process idle time?

  5. 5

    How do I use a regex in a shell script?

  6. 6

    How do I return a HTTP 404 status code from a SPA?

  7. 7

    Why do I get `Only one connection receive subscriber allowed` when the response has an HTTP error status code?

  8. 8

    How do I get only the extension of a file when writing a shell command for a context menu

  9. 9

    How do I close stdin in a shell script?

  10. 10

    How do I script a filecount in android shell?

  11. 11

    Bash shell script: how do I exit and restart the script?

  12. 12

    How do I get an HTTP status code from TTransport exceptions?

  13. 13

    How do I get my shell script to automatically accept prompts?

  14. 14

    How do I wait for a file in the shell script?

  15. 15

    How to split text and get only array value by using shell script?

  16. 16

    MVC Site getting 500 error only on server, how do I get more info on error?

  17. 17

    How do I write a shell script to get a given process idle time?

  18. 18

    How do I handle switches in a shell script?

  19. 19

    How do I get an acceptable last modified timestamp for my Jekyll site using a shell script?

  20. 20

    How do I get to the shell on ubuntu tablet?

  21. 21

    How do I write shell script for Redhat Linux bash shell

  22. 22

    How do I log into bash shell only?

  23. 23

    How do I get screen resolution in a shell script on Linux

  24. 24

    Why do I get different exit status for ps | grep in a script?

  25. 25

    How do i run a shell script, in a shell script with the menu dialog?

  26. 26

    How do I get XDG_CONFIG_HOME from a shell script / command line?

  27. 27

    How do you set the exit status of a shell script?

  28. 28

    How do I get the 'application_uris' From a Bluemix Cloudfoundry App in shell script?

  29. 29

    How do I add a line to the shell script?

HotTag

Archive