scripts

Various scripts for UNIX-like systems
git clone https://git.tronto.net/scripts
Download | Log | Files | Refs | README

dmenu-unmount (1068B)


      1 #!/bin/sh
      2 
      3 # Prompts selection for mounted devices and unmounts the selected one
      4 # Requires: udevil, dmenu (or similar), notify-send or similar (optional)
      5 
      6 # Usage: dmenu-unmount [-m menu] [-w writer]
      7 # Example: dmenu-unmount -m slmenu -w echo
      8 
      9 menu=dmenu
     10 #writeout=notify-send
     11 writeout="notify push"
     12 
     13 usage() {
     14 	echo "Usage: dmenu-unmount [-m MENU] [-w WRITER]"
     15 	echo "Example: dmenu-unmount -m slmenu -w echo"
     16 }
     17 
     18 while getopts "m:w:" opt; do
     19 	case "$opt" in
     20 		m)
     21 			menu="$OPTARG"
     22 			;;
     23 		w)
     24 			writeout="$OPTARG"
     25 			;;
     26 		*)
     27 			usage
     28 			exit 1
     29 			;;
     30 	esac
     31 done
     32 shift $((OPTIND - 1))
     33 
     34 listdev=$(grep "media" /proc/mounts | sed 's/\/media.*\///g' | \
     35           awk '{print $1" ("$2")"}')
     36 
     37 if [ -z "$listdev" ]; then
     38 	$writeout "Nothing to unmount"
     39 else
     40 	seldev=$(echo "$listdev" | $menu | awk '{print $1}')
     41 	if [ -n "$seldev" ]; then
     42 		udevil unmount "$seldev"
     43 		failed=$(udevil info "$seldev" | grep mounted | awk '{print $3}')
     44 		if [ "$failed" -eq 1 ]; then
     45 			$writeout "Unmount FAILED" "Device is still mounted!"
     46 		else
     47 			$writeout "Device unmounted"
     48 		fi
     49 	fi
     50 fi