unmount.sh 1002 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/sh
  2. # Global variables
  3. TITLE="Unmount Utility"
  4. COLUMNS=3 # TARGET,SOURCE,FSTYPE
  5. #IFS=$'\n'
  6. # Populate list of unmountable devices
  7. deviceList=($(findmnt -Do TARGET,SOURCE,FSTYPE | grep -e "sd[b-z]"))
  8. deviceCount=$((${#deviceList[@]} / $COLUMNS))
  9. # Start of program output
  10. echo $TITLE
  11. # Display list of devices that can be unmounted
  12. for ((device=0; device<${#deviceList[@]}; device+=COLUMNS))
  13. do
  14. printf "%4s) %-25s%-13s%-10s\n"\
  15. "$(($device / $COLUMNS))"\
  16. "${deviceList[$device]}"\
  17. "${deviceList[$(($device + 1))]}"\
  18. "${deviceList[$(($device + 2))]}"
  19. done
  20. printf "%4s) Exit\n" "x"
  21. # Get input from user
  22. read -p "Choose a menu option: " input
  23. # Input validation
  24. if [ "$input" = "X" ] || [ "$input" = "x" ]
  25. then
  26. echo "Exiting"
  27. exit 0
  28. fi
  29. if (( $input>=0 )) && (( $input<$deviceCount ))
  30. then
  31. echo "Unmounting: ${deviceList[$(($input * $deviceCount))]}"
  32. sudo umount "${deviceList[$(($input * $deviceCount))]}"
  33. exit 0
  34. else
  35. echo "Invalid menu choice"
  36. exit 1
  37. fi