Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ##################################################################################################
  4. #
  5. #
  6. # Automatic copy files from USB to computer on connect!
  7. #
  8. #
  9. # Author: Abdul Noushad Sheikh ('abdulnine7' on GitHub)
  10. #
  11. #
  12. ##################################################################################################
  13.  
  14. setup=false
  15.  
  16. die() {
  17. echo -e "\033[1;31m$@\033[0m"
  18. exit 1
  19. }
  20.  
  21. message(){
  22. export DISPLAY=:0
  23. notify-send "$@" -t 500
  24. }
  25.  
  26. message "hi"
  27.  
  28. #Setup for running
  29. if $setup; then
  30.  
  31. if [[ $(/usr/bin/id -u) -ne 0 ]]; then
  32. die "For Setup run as root..."
  33. fi
  34.  
  35. user=`who | cut -d ' ' -f 1`
  36.  
  37. sudo touch /etc/udev/rules.d/pendrive.rules
  38. sudo echo "ACTION==\"add\",KERNEL==\"sd*\", ATTRS{idVendor}==\"*\", ATTRS{idProduct}==\"*\", RUN+=\"/usr/bin/sudo -u $user /home/$user/pendrive.sh\"" > /etc/udev/rules.d/pendrive.rules
  39. sudo /etc/init.d/udev restart
  40. sudo udevadm control --reload-rules
  41. die "Setup successful"
  42. fi
  43.  
  44. #Getting mountpoint of the usb drives
  45. sleep 5
  46. usb_dirs=(`lsblk -l | grep -e part -e disk | grep sd[bc] | awk '{ printf $7 "\n"; }'`)
  47.  
  48. if [ ${#usb_dirs[@]} -eq 0 ]; then
  49. die "\nNo USB Device connected...\n"
  50. fi
  51.  
  52. #Usb Device connected message
  53. message "Usb Device Connected"
  54.  
  55. #Copiying the data from pendrive
  56.  
  57. for((i=0;i<${#usb_dirs[@]};i++))
  58. do
  59. echo -e "\033[1;33mCopying from... ${usb_dirs[$i]}"
  60. mkdir ~/copied_data 2> /dev/null
  61. cp -r ${usb_dirs[$i]} ~/copied_data
  62. done
  63.  
  64. #Copiying done
  65. message $(date +"%A %d-%B-%Y");
  66. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement