Following a URL in a BASH function


I had need to ensure a Url was valid.. So i decided to write a function that I could use in my bash session for just that type of thing:

followedUrlValid()
  usage()
  {
    echo " -- Usage for followedUrlValid --- 
    \$1 = the account you wish to login to
      looks for the http_code from getting the url if valid returns true if not returns false
      if the url has redirects it'll follow the redirects
    "
  }
  if [ -z "$1" ]; then
    echo >&2 'Need 1 parameters which are not empty'
    usage
    return 1
  fi
   returnval='false'
   value=$(curl -o /dev/null -LIsw '%{http_code}\n' $1)
   if [ "$value" = 200 ]; then
    returnval='true'
  fi
  echo "$returnval"

Below you can see two uses of this function in your bash/zsh prompt

followedUrlValid https://google.com
true

followedUrlValid https://invalidurl.okiedokie
false

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s