blob: f7f1ecb521b92f7c79dbb5f3032d98deb3d19724 (
plain)
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
76
77
78
79
80
81
82
83
84
85
86
|
#!/bin/sh
tmp="$(mktemp)"
test "$(id -u)" != "0" && sudo="sudo"
die ()
{
echo "$@" >&2
}
# Read one character
read_char ()
{
die -n ">"
old_stty_cfg=$(stty -g)
stty raw
dd ibs=1 count=${1:-1} 2> /dev/null
stty $old_stty_cfg
die ""
}
# mount the device with $1 as the choice
mount ()
{
dev="$(grep "^$1\." "$tmp" | cut -f 2- -d ' ')"
test -z "$dev" && exit
die "Mounting /dev/$dev on /media/$dev"
mkdir -p /media/$dev
$sudo mount /dev/$dev /media/$dev > /dev/null 2>&1 ||
return 1
}
# umount the device with $1 as the choice
umount ()
{
printf "$1" | grep -q "[0-9]" || exit 1
mountpoint="$(sed -n "${1}p" "$tmp" |
awk '{print $3}')"
test -z "$mountpoint" && exit 1
die "Unmounting $mountpoint"
$sudo umount "$mountpoint" ||
return 1
}
# print lsblk, use $1 to print only devices with mountpoints or without
pr_lsblk ()
{
clear
lsblk -o name,size,type,mountpoint
die "───────────────────────────────────"
lsblk --ascii -o name,mountpoint |
grep '^.-' |
while read line
do
words="$(printf "$line" | wc -w)"
test $words -eq ${1:-1} && continue
printf "%s\n" "$line"
done |
cut -f 2- -d "-" |
awk '{print NR ". " $0}' |
tee "$tmp" >&2
}
cleanup ()
{
rm -f "$tmp"
}
trap cleanup EXIT INT
die "m(ount) u(mount) ?"
choice="$(read_char)"
case $choice in
"m") i=2; cmd=mount ;;
"u") i=1; cmd=umount ;;
*) exit 1 ;;
esac
pr_lsblk $i
choice="$(read_char)"
$cmd $choice &&
die "Successful." ||
die "Failed."
|