Friday, 27 July 2012

Bash script - curl and analyse http response code

Basic bash script that use curl and analyse http response code. Script returns 0 (ok) or error (1) bash exit code status, so that it can be used / called from another, more complicated script.
script code name: curl-head.sh

# -----------------------------------------

#!/bin/bash
CURL_URL=$1
DTIME=`date +%Y%m%d%M%S`
CURL_TMP="/tmp/curl-head-bash-script-${DTIME}.tmp"

curl -I -s ${CURL_URL} > ${CURL_TMP}
CURL_TST=`cat ${CURL_TMP} | grep -c "200 OK"`

if [ ${CURL_TST} -eq 1 ]
then
        STATUS=0
else
        STATUS=1        # http header response non 200, error
fi

if [ -f ${CURL_TMP} ]; then
        rm -f ${CURL_TMP}
fi

exit ${STATUS}

# -----------------------------------------

No comments:

Post a Comment