Advertisement
Guest User

adadaadaddq

a guest
Dec 10th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.74 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3.  
  4. #This function tests if a line is empty or a comment
  5. #returns:(0 if line is not empty/comment)&&(1 if the line is a comment or empty)
  6. function line_is_valid () {
  7.     if [[ $1 == \#* ]] ||  [[ -z $1 ]]
  8.     then
  9.         return 1
  10.     else
  11.         return 0
  12.     fi 
  13. }
  14.  
  15.  
  16. #This function is used to initialize a url inside source.If the url doesnt exist in source.txt ,it is added
  17. # and the appropriate stdout is printed
  18. #returns:(0 if the url was initialized)&&(1 if the url has already been initialized)
  19. function init () {
  20.     if ! grep -q "$1" source.txt
  21.     then
  22.         if curl -s "$1" > temp.txt
  23.         then
  24.             md5="$(md5sum temp.txt | awk '{ print $1 }')"
  25.             echo $md5 $1 >> source.txt
  26.             echo "$1 INIT" 
  27.             return 0;
  28.         else
  29.             echo "$1 FAILED"
  30.        
  31.         fi
  32.     fi
  33.     return 1;
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40. function search_update() {
  41.    
  42.     if ! grep -q "$1" source.txt
  43.     then
  44.         if curl -s "$1" > temp.txt
  45.         then
  46.             less temp.txt
  47.             md5="$(md5sum temp.txt | awk '{ print $1 }')"
  48.             echo "HASH: $md5 URL:$1 ">> errorlog.txt
  49.             echo $md5 $1 >> source.txt
  50.             echo "$1 INIT" 
  51.         else
  52.             echo "$1 FAILED"
  53.         fi
  54.    
  55.     else
  56.    
  57.         if curl -s "$1" > temp.txt
  58.         then
  59.             less temp.txt
  60.             newHash="$(md5sum temp.txt | awk '{ print $1 }')"
  61.             prevHash="$(grep "$1" source.txt | awk '{ print $1 }')"
  62.             echo "New=$newHash prev=$prevHash Url = $1" >> errorlog.txt
  63.             if ! [ "$newHash" = "$prevHash" ]
  64.             then
  65.                 echo $1
  66.                 sed -i "s/$prevHash/$newHash/" source.txt
  67.             fi 
  68.         else
  69.             echo "$1 FAILED"
  70.         fi
  71.     fi
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80. #Making sure that source.txt exists
  81. if ! [[ -r source.txt ]]
  82. then
  83.     touch source.txt
  84. fi
  85. #fetching all urls
  86. while read row
  87. do
  88.     if  line_is_valid $row
  89.     then
  90.     urls="$urls $row "
  91.     fi
  92. done < $1
  93.  
  94.  
  95.  
  96. for url in $urls
  97. do
  98.     search_update $url &   
  99. done
  100. wait
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement