Advertisement
Guest User

delay_copy.sh

a guest
Oct 8th, 2011
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.88 KB | None | 0 0
  1. #!/bin/sh
  2. #--[ This is a utility to copy/list files on a drive in cases     ]--#
  3. #--[ where there is only one USB port that a keyboard/mouse       ]--#
  4. #--[ is utilizing.                                                ]--#
  5. #--[                                                              ]--#
  6. #--[ Is open source and such, definately edit it as needed.       ]--#
  7. #--[ Written by Lucas C.                                          ]--#
  8.  
  9. #--------------[ Various variables assigned here ]---------------#
  10.  
  11. waitTime=10
  12. userIn=$@
  13. timeCount=$waitTime
  14. currentDir=$(pwd)
  15.  
  16. #--------------[ Functions defined here ]---------------#
  17.  
  18. function mountDrive(){
  19. echo "Mounting drive..."
  20. mkdir -p /tmp/usbDrive
  21. mount /dev/sdb /tmp/usbDrive
  22. }
  23.  
  24. function unmountDrive(){
  25. echo "Unmounting drive..."
  26. umount /dev/sdb
  27. echo "Drive unmounted."
  28. }
  29.  
  30. function timer(){
  31. echo "You have $waitTime seconds to eject keyboard/enter USB drive"
  32. while [ "$timeCount" != "0" ]
  33. do
  34.     echo "$timeCount"
  35.     sleep 1
  36.     (( timeCount-- ))
  37. done
  38. }
  39.  
  40. function helpPl0x(){
  41. echo "Help section for the delay_copy utility"
  42. echo ""
  43. echo "Syntax: delay_copy [option]"
  44. echo "Or if root permissions are needed: sudo delay_copy [option]"
  45. echo ""
  46. echo "Command line arguments are:"
  47. echo "  -cp or --copy - Copy files from the drive to the current working directory"
  48. echo "  -ls or --list - List files from the drive"
  49. echo "   -h or --help - This help page"
  50. echo "  -ct or --copyto - copy file(s) to the USB drive"
  51. echo ""
  52. echo "This utility mounts the USB drive at /tmp/usbDrive."
  53. }
  54.  
  55. function reconnectAlert(){
  56. echo "Devices such as mouse/keyboard can now be reconnected."
  57. }
  58.  
  59. #---------------[ Program starts here ]--------------#
  60.  
  61. if [ "$(whoami)" != "root" ]
  62. then
  63.     echo "Note: You may need to run this utility as root for it to function properly."
  64. fi
  65.  
  66. if [ "$userIn" = "-cp" ] || [ "$userIn" = "--copy" ]
  67. then
  68.     echo "Enter a filename to copy"
  69.     read -p "> " copyUsbDrive
  70.     timer
  71.     mountDrive
  72.     echo "Copying files..."
  73.     cd /tmp/usbDrive/
  74.     cp $copyUsbDrive "$currentDir"
  75.     cd "$currentDir"
  76.     echo "Files copied."   
  77.     unmountDrive
  78.     reconnectAlert
  79.  
  80. elif [ "$userIn" = "-ct" ] || [ "$userIn" = "--copyto" ]
  81. then
  82.     echo "Enter file(s) to copy to the drive"
  83.     read -p "> " copyToDrive
  84.     echo "Enter location on drive to copy files to"
  85.     read -p "> " copyToLocation
  86.     timer
  87.     mountDrive
  88.     echo "Copying files..."
  89.     cp $copyToDrive /tmp/usbDrive/"$copyToLocation"
  90.     echo "Files copied."
  91.     unmountDrive
  92.     reconnectAlert
  93.  
  94. elif [ "$userIn" = "-ls" ] || [ "$userIn" = "--list" ]
  95. then
  96.     echo "Enter a location on the drive to list, leave blank to list contents of drive"
  97.     read -p "> " listUsbDrive
  98.     timer
  99.     mountDrive
  100.     echo "Contents: [" ; echo ""
  101.     ls /tmp/usbDrive/"$listUsbDrive"
  102.     echo "" ; echo "]" ; echo ""
  103.     unmountDrive
  104.     reconnectAlert
  105.  
  106. elif [ "$userIn" = "-h" ] || [ "$userIn" = "--help" ]
  107. then
  108.     helpPl0x
  109.  
  110. else
  111.     helpPl0x
  112. fi
  113.  
  114. exit 0
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement