# Bash script to monitor Webserver php script

Below is a script I wrote today to use CURL to monitor the length of various HTTP server outputs and output a response if this is different than 103 characters. This is my first bash script, so I had some reading/experimenting to do and it is quite rough. The idea is that if there are no errors, my php script will ouput exactly 103 characters. If there are errors, this will be increased.

IDScript

#!/bin/bash
# Check for missing IDs - greater output than 103 characters
cd "/media/SMALL/scripts"

if [[ -z "$1" ]] # check if script arguement exists then a=$(./IDscript "server");
a=$(./IDscript "server2")$a;  #concatenate with previous output
a=$(./IDscript "server3")$a;

lenA=$(expr length "$a");

if [ $lenA -eq 0 ] #check length of A is empty then echo "No errors"; else mail -s "TID error" myEmail@company.org <<< "There are errors:"$a; # send the e-mail
fi
exit;
fi

#create random number from 50 to 99
r=$(($RANDOM % 50 + 50 ));
#bypass HTTP proxy with random number
testURL=$1"/checkError?which=IDError&rand="$r;

curlOut=$(curl -s "$testURL");
curlLength=$(expr length "$curlOut");

if [ $curlLength -ne 103 ] #103 char is response length when no missing IDs exist then echo "length is not 103:"$testURL;
echo -e "\n";
fi


• Checking for a 103-byte response is rather weird. I would recommend that you check either the HTTP status code (the return value of curl --fail --silent) or verify if the output matches a particular string or pattern. – 200_success Dec 12 '13 at 23:07

#!/bin/bash


Minor, still good to know, this is not portable, f.e. BSDs will choke on it. This might not be an issue if you only intent to use it on Linux, but if you ever release script into the wild, you should consider using a portable shebang line.

#!/usr/bin/env bash


if [[ -z "$1" ]] # check if script arguement exists then  I'm a friend of the one-line-style, but that is for sure a pure personal preference. if [[ -z "$1" ]]; then # check if script arguement exists


a=$(./IDscript "server");  From the context of this I can say two things: 1. that variable needs a better name 2. that script needs a better name It is not obvious what the script does and neither what the variable will hold. a=$(./IDscript "server");


In a shell script, there's no need to close every line with a semicolon. You can do it, but it's not necessary.

lenA=$(expr length "$a");


If you're going to use Bash, you can also use Bash syntax:

lenA=${#a}  Or shortened to: if [${#a} -eq 0 ] #check length of A is empty
then


if [[ -z "$1" ]]  What I made a habit out of in the last time is extracting arguments into named variables so that the script is easier to read. webAddress=$1

if [[ -z "$webAddress" ]] ... testURL=$webAddress"/checkError?which=IDError&rand="$r;  This is of course not always useful, but pretty neat if you're going to use $1 in multiple places, as the script becomes easier to read.

echo -e "\n";


This can be shortened.

echo

• But otherwise I guess it seems ok... – Menios Dec 12 '13 at 12:12
• @meewoK: I don't see something wrong with it other then that (oh wait, there's something tiny). A few more comments (explaining what's going on) would be nice, but depending on your environment that might be absolutely unnecessary. – Bobby Dec 12 '13 at 12:55