Advertisement
Guest User

wget / extract script with logging and error handling

a guest
Feb 19th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. function catcherror {
  4. "$@"
  5. STATUS=$?
  6. if [ $STATUS -ne 0 ]; then
  7. echo Error with $@ $TS >> $LOG;
  8. fi
  9.  
  10. return $STATUS
  11. }
  12.  
  13. function cleanup {
  14. SUCCESS=-1
  15. if [ $ALT == 0 ]; then #remove the .gz
  16. catcherror rm $GZ
  17. if [ $STATUS -ne 0 ]; then
  18. SUCCESS=0
  19. else
  20. SUCCESS=1
  21. fi
  22. else #remove the .zip
  23. echo Alternate download was used $TS >> $LOG
  24. catcherror rm $ZIP
  25. if [ $STATUS -ne 0 ]; then
  26. SUCCESS=0
  27. else
  28. SUCCESS=1
  29. fi
  30. fi
  31. if [ $SUCCESS == 1 ]; then
  32. echo Complete $TS >> $LOG
  33. else
  34. echo Failed to remove archive file $TS >> $LOG
  35. fi
  36. return
  37. }
  38.  
  39. ALT=0 # flag that alternate download is to be attempted
  40. ALTFAIL=-1 # flag that alternate download failed
  41. LOG=/root/scripts/p2p-update.log # path of log
  42. GZ=/root/scripts/.lvl1/level1.gz # path of .gz
  43. ZIP=/root/scripts/.lvl1/level1.zip # path of .zip
  44. BL=/root/.config/transmission/blocklists # path of blocklist dir
  45. DL=/root/scripts/.lvl1 # path of download location
  46. TS=`date` # timestamp
  47.  
  48. catcherror wget "http://list.iblocklist.com/?list=bt_level1&fileformat=p2p&archiveformat=gz" -O $GZ
  49. if [ $STATUS -ne 0 ]; then #wget failed first try
  50. ALT=1 #try alternate means
  51. else #wget worked first try
  52. catcherror file-roller -e $BL $GZ
  53. if [ $STATUS -ne 0 ]; then #file-roller failed to extract the list
  54. ALT=1 #try alternate means
  55. else #everything worked first try
  56. cleanup
  57. fi
  58. fi
  59. if [ $ALT == 1 ]; then #try to wget .zip
  60. catcherror wget "http://list.iblocklist.com/?list=bt_level1&fileformat=p2p&archiveformat=zip" -O $ZIP
  61. if [ $STATUS -ne 0 ]; then #wget of .zip failed
  62. ALTFAIL=1
  63. else #wget of .zip worked
  64. catcherror unzip -o -d $BL $ZIP #try to unzip .zip
  65. if [ $STATUS -ne 0 ]; then #unzip failed
  66. ALTFAIL=1
  67. else #everything worked second try
  68. cleanup
  69. fi
  70. fi
  71. fi
  72. if [ $ALTFAIL == 1 ]; then
  73. echo Alternate means failed. $TS >> $LOG
  74. exit 1
  75. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement