petits-scripts-persos/interractive_mount

85 lines
1.9 KiB
Plaintext
Raw Normal View History

2022-02-17 23:08:41 +00:00
#!/bin/sh
function usage () {
echo "Usage:"
2022-02-18 15:05:36 +00:00
echo "$0 <action> [mode]"
2022-02-17 23:15:44 +00:00
echo "action is either mount or unmount"
2022-02-18 15:05:36 +00:00
echo "action is text or nothing"
2022-02-17 23:08:41 +00:00
}
# If no display found, full text interface. use dmenu and dunstify else.
2022-02-18 15:05:36 +00:00
if [ "$2" = text ] || [ -z "$DISPLAY" ] ; then
2022-02-17 23:08:41 +00:00
function notify () {
2022-02-18 15:05:36 +00:00
echo -e "== $1 ==\n$2"
2022-02-17 23:08:41 +00:00
}
function ask () {
2022-02-18 15:05:36 +00:00
# $1 is the title
# $2 is the \n separated list of choices
# $3 is the number of choices
IFS=$'\n' read -r -d '' -a args <<< $(echo -e "$2")
dialog --no-items --menu "$1" 15 40 4 "${args[@]}" 2>&1 >/dev/tty
2022-02-17 23:08:41 +00:00
}
else
function notify () {
dunstify "$1" "$2"
}
function ask () {
2022-02-18 15:05:36 +00:00
# See interface specification up
echo -en "$2" | dmenu -i -l "$3" -p "$1"
2022-02-17 23:08:41 +00:00
}
fi
if [ "$1" = "mount" ] ; then
action=mount
elif [ "$1" = umount ] || [ "$1" = unmount ] ; then
action=umount
else
usage
exit 1
fi
mounted=""
umounted=""
mounted_nb=0
umounted_nb=0
# List mounted and unmounted disks
while read disk size mountpoint ; do
# Filters. Add yours
[[ "$disk" = /dev/sda* ]] && continue
[ "$disk" = /dev/mapper/cryptroot ] && continue
# Selection
if [ -z "$mountpoint" ] ; then
umounted="$umounted$disk $size\n"
((umounted_nb++))
else
mounted="$mounted$disk $size $mountpoint\n"
((mounted_nb++))
fi
done <<< $(lsblk -prno NAME,SIZE,MOUNTPOINTS)
# Prompt user for disk action
if [ "$action" = "mount" ] ; then
2022-02-18 15:05:36 +00:00
if [ -z "$umounted" ] ; then
notify "Mounting" "Nothing to mount"
else
read disk size <<< $(ask 'Mount' "$umounted" "$umounted_nb")
if [ -b "$disk" ] ; then
notify "Mounting $disk" "$(udisksctl mount -b "$disk" 2>&1)"
fi
2022-02-17 23:08:41 +00:00
fi
elif [ "$action" = "umount" ] ; then
2022-02-18 15:05:36 +00:00
if [ -z "$mounted" ] ; then
notify "Unmounting" "Nothing to unmount"
else
read disk size mountpoint <<< $(ask 'Unmount' "$mounted" "$mounted_nb")
if [ -b "$disk" ] ; then
notify "Unmounting $disk" "$(udisksctl unmount -b "$disk" 2>&1)"
fi
2022-02-17 23:08:41 +00:00
fi
fi