Advertisement
stubborn_d0nkey

Shell function to check if website has changed version 2

Feb 3rd, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.92 KB | None | 0 0
  1. #######################################################
  2. #######################################################
  3. ### usage: webch [-ciqr] url
  4. ### usage: webch [-i INTERVAL] [-r REPEAT] [-q] [-c "COMMAND(S) TO EXECUTE"] url
  5. ### options:
  6. ### -i, --interval : time (in seconds) to wait between downloads/page checks  (default=30)
  7. ### -r, --repeat : how many times to repeat (ie. how many changes till exit) (default=1)
  8. ### -q, --quiet : turns off beep after each change
  9. ### -c, --command : command(s) to execute after each change (default=echo "It changed!")
  10. #####################################################
  11. #
  12. #
  13. function webch(){
  14.    
  15.     inter=30
  16.     repeat=1
  17.     beep=true
  18.     com='echo "It changed!"'   
  19.     OPTS=`getopt -o qi:r:-c: -l quiet,interval:,repeat:,command: -- "$@"`
  20.  
  21.     if [ $? -ne 0 ];
  22.     then
  23.  
  24.         return 1
  25.  
  26.     fi
  27.    
  28.     eval set -- "$OPTS"
  29.    
  30.     while true ; do
  31.         case "$1" in
  32.             -q|--quiet) beep=false; shift;;
  33.             -i|--interval) inter=$2 ; shift 2;;
  34.             -r|--repeat) repeat=$2; shift 2;;
  35.             -c|--command) com=$2; shift 2;;
  36.             --) shift; break;;
  37.             *) break;;
  38.         esac
  39.     done
  40.     url=$1                  #Passing the first leftover argument to the variable url
  41.    
  42.     f1=/tmp/.`mcookie`     
  43.     f2=/tmp/.`mcookie`     
  44.  
  45.     wget -q -O $f1 $url        
  46.    
  47. while [ $repeat -ne 0 ]; do
  48.    
  49.     tmp=0                   #Setting tmp to 0
  50.     while [ $tmp -eq 0 ] ; do
  51.         sleep $inter            #Waiting before fetching the page
  52.         wget -q -O $f2 $url     #Fetching page
  53.         cmp -s $f1 $f2          #Checking first fetch vs last. cmp exits with 0 if files are the same, 1 if they are different
  54.         tmp=$?              #Setting what cmp exited with to tmp
  55.         echo whileloop
  56.     done
  57.     rm $f1
  58.     f1=$f2
  59.     f2=/tmp/.`mcookie`
  60.     if [ $tmp -gt 1 ]           #Checks if cmp gave an error
  61.     then
  62.         echo "An error occured"
  63.         return $tmp
  64.     fi
  65.    
  66.     if $beep;              
  67.     then
  68.         echo -en "\007"    
  69.     fi
  70.     eval $com
  71.    
  72.     let repeat=repeat-1
  73. done
  74.    
  75.     rm $f1              #removing files that are nolonger needed
  76.     return 0
  77. }
  78. ###########################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement