-
Notifications
You must be signed in to change notification settings - Fork 3
/
dmenu-udevil.sh
executable file
·75 lines (60 loc) · 1.71 KB
/
dmenu-udevil.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
DMENU=${DMENU:-dmenu}
DEV_LABEL="/dev/disk/by-label/"
TMP="/tmp/dmnt-udevil-$(date +%s)"
trap "rm -f $TMP" EXIT
opt_mount_type=0
opt_ignore_filter=0
opt_notify=0
udevil_cmd="mount"
usage() {
cat <<-EOF
usage: dmenu-udevil [-mudihn]
-m Mount devices
-u Unmount devices
-d Select by device rather than by label
-i Ignore filter and list all devices in /dev (with -d)
-n Pass udevil output to notify-send
-h Print help
EOF
}
dmenu_mnt() {
if [[ $opt_mount_type -eq 1 ]]; then
prompt="$udevil_cmd by-device:"
if [[ $opt_ignore_filter -eq 0 ]]; then
res="$(find /dev -maxdepth 1 -not -type d -name "s[dr]*" -or -name "hd*" | cut -d'/' -f3 | ${DMENU} -p "$prompt")"
else
res="$(find /dev -maxdepth 1 -not -type d | cut -d'/' -f3 | ${DMENU} -p "$prompt")"
fi
path="/dev/$res"
[[ -z $res ]] && echo "Cancelled." && exit
else
prompt="$udevil_cmd by-label:"
res="$(find $DEV_LABEL* | cut -d'/' -f5 | ${DMENU} -p "$prompt")"
path="$DEV_LABEL/$res"
[[ -z $res ]] && echo "Cancelled." && exit
fi
udevil $udevil_cmd "$path" > "$TMP" 2>&1
exitc=$?
if [[ $opt_notify -eq 1 ]]; then
case $exitc in
0) urgency="normal";;
*) urgency="critical";;
esac
notify-send -u $urgency "$(<$TMP)"
else
cat "$TMP"
fi
}
while getopts ':mudhin' opt; do
case "$opt" in
m) ;;
u) udevil_cmd="umount";;
d) opt_mount_type=1;;
i) opt_ignore_filter=1;;
h) usage && exit;;
n) opt_notify=1;;
/?) echo "Unrecognized command: $OPTARG";;
esac
done
dmenu_mnt && exit