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 &lt;&lt;&lt; "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&amp;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


Comments and suggestions are welcome!
#!/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:


that variable needs a better name
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&amp;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