3 # Description: Toggle mount status of a device using pmount
4 # If the device is not mounted, it will be mounted.
5 # If the device is mounted, it will be unmounted and powered down.
7 # Dependencies: lsblk, pmount (optional), udisks2
9 # Usage: Runs `lsblk` on 'l', exits on 'Return`.
12 # - The script uses Linux-specific lsblk to list block devices. Alternatives:
13 # macOS: "diskutil list"
14 # BSD: "geom disk list"
15 # - The script uses udisksctl (from udisks2) to power down devices. This is also Linux-specific.
16 # Users on non-Linux platforms can comment it and use an alternative to power-down disks.
18 # Shell: POSIX compliant
19 # Author: Arun Prakash Jana
21 prompt="device name [e.g. sdXn] ('l'ist, 'q'uit): "
25 printf "\nEnsure you aren't still in the mounted device.\n"
29 while [ -n "$dev" ]; do
30 if [ "$dev" = "l" ]; then
32 elif [ "$dev" = "q" ]; then
35 if grep -qs "$dev " /proc/mounts; then
36 sync "$(lsblk -n "/dev/$dev" -o MOUNTPOINT | sed "/^$/d")"
37 if type pumount >/dev/null 2>&1; then
41 udisksctl unmount -b "/dev/$dev" --no-user-interaction
44 if [ $exit_code -eq 0 ]; then
45 echo "/dev/$dev unmounted."
46 if udisksctl power-off -b "/dev/$dev" --no-user-interaction; then
47 echo "/dev/$dev ejected."
50 elif [ -b "/dev/$dev" ]; then
51 if type pmount >/dev/null 2>&1; then
55 udisksctl mount -b "/dev/$dev" --no-user-interaction
58 if [ $exit_code -eq 0 ]; then
60 echo "/dev/$dev mounted to $(lsblk -n "/dev/$dev" -o MOUNTPOINT | sed "/^$/d")."
63 echo "/dev/$dev does not exist or is not a block device."