Posted By

swetoast on 05/11/16


Tagged

selfupdater


Versions (?)

Selfupdating Script


 / Published in: Bash
 

Simple script that updates itself upon launch then lauches whatever else thats included afterwards works well in almost any linux system tested on busyboxes and Debian systems

  1. #!/bin/sh
  2. SELF=$(basename $0) #uses the same name as the file
  3. UPDATE_BASE=https://gitlab.com/swe_toast/self-update/raw/master/ #dont link directly to file link to the folder
  4. UPDATE_SELF=${UPDATE_SELF:-1}
  5.  
  6. self_updater () {
  7. echo "Performing self-update... Please wait..."
  8. if ! wget --quiet --output-document="$0.tmp" $UPDATE_BASE/$SELF ;
  9. then echo "Failed downloading new version!"
  10. echo "File requested: $UPDATE_BASE/$SELF"
  11. exit 1
  12. fi
  13.  
  14. OCTAL_MODE=$(stat -c '%a' $0)
  15. case ${OCTAL_MODE:--1} in
  16. -[1] ) echo "Failed: Error : OCTAL_MODE was empty"; exit 1 ;;
  17. 777|775|755 ) echo "OCTAL mode set:${OCTAL_MODE}" ;;
  18. * ) echo "Failed: Error in OCTAL_MODEs, found value=${OCTAL_MODE}" exit 1 ;;
  19. esac
  20.  
  21. if ! chmod $OCTAL_MODE $0.tmp ;
  22. then echo "Error on chmod $OCTAL_MODE %0.tmp from $UPDATE_BASE/$SELF, can't continue"
  23. exit 1
  24. fi
  25.  
  26. cat > /tmp/updatescript.sh << EOF
  27. #!/bin/sh
  28. if mv "$0.tmp" "$0";
  29. then echo "Update complete.."
  30. exec env UPDATE_SELF=0 /bin/sh "$0"
  31. rm \$0
  32. else echo "Update failed.."
  33. fi
  34. EOF
  35. echo "Inserting update process..."
  36. exec /bin/sh /tmp/updatescript.sh; rm /tmp/updatescript.sh
  37. }
  38.  
  39. check_update () { if [ ${UPDATE_SELF} -ne 0 ]; then self_updater; fi }
  40.  
  41. check_update

Report this snippet  

You need to login to post a comment.