Advertisement
stubborn_d0nkey

Shell function to check if website has changed

Feb 1st, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.20 KB | None | 0 0
  1. function webch(){
  2.    
  3.     f1=/tmp/.`mcookie`          #File to save webpage in the first time
  4.     f2=/tmp/.`mcookie`          #File to save webpage in to compare to first download
  5.     url=$1                  #Passing the first argument to the variable url
  6.     wget -q -O $f1 $url         #Fetching the page for the first time
  7.     tmp=0                   #Setting tmp to 1
  8.     if [ -z $2 ];               #Checking if time to wait has been supplied (in seconds)
  9.     then
  10.         time=30             #Default if not supplied
  11.         else
  12.         time=$2             #Setting time to what was passed
  13.     fi
  14.    
  15.     while [ $tmp -eq 0 ] ; do
  16.         sleep $time         #Waiting before fetching the page
  17.         wget -q -O $f2 $url     #Fetching page
  18.         cmp $f1 $f2         #Checking first fetch vs last. cmp exits with 0 if files are the same, 1 if they are different
  19.         tmp=$?              #Setting what cmp exited with to tmp
  20.     done
  21.     rm $f1 $f2              #removing files that are nolonger needed
  22.    
  23.     if [ $tmp -gt 1 ]           #Checks if cmp gave an error
  24.     then
  25.         echo "An error occured"
  26.         exit 127
  27.     fi
  28.    
  29.     if [ -z $3 ];               #Checks if an action has been supplied
  30.     then
  31.         echo "It changed!"      #Default output if no action has been supplied
  32.         echo -en "\007"         #Beeps once to notify that a change has occured
  33.     else
  34.         ${@:3}              #Completes supplied command(s)
  35.     fi
  36.     return 0
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement