Thursday, October 3, 2019

Check URL Status with Curl

if [ $# -lt 1 ]; then
  echo "%%TCS-E-PARAMETER, url is not found."
  exit $LINENO
fi

tgtUrl=$1

urlRexp="https{0,1}://.{1}"

if [[ ! $tgtUrl =~ $urlRexp ]]; then
  echo "%%TCS-E-PARAMETER, target url must start with https://"
  exit $LINENO
fi

pid=$$


checkStatus=`curl -o /dev/null --silent --head --write-out '%{http_code}\n' $tgtUrl`

case $checkStatus in
        2*)
                echo "$checkStatus OK: $tgtUrl"
                exit 0
                ;;
        3*)
                echo "$checkStatus Redirection Happened to reach: $tgtUrl"
                exit 0
                ;;
        4*)
                echo "$checkStatus Client error to reach: $tgtUrl"
                exit $LINENO
                ;;
        5*)
                echo "$checkStatus Server error to reach: $tgtUrl"
                exit $LINENO
                ;;
        *)
                echo "$checkStatus No valid HTTP response code to: $tgtUrl"
                exit $LINENO
                ;;
esac

No comments:

Post a Comment