sistema_progs

Programas para customizar o meu entorno de traballo nos meus equipos persoais
Log | Files | Refs

nmount (1487B)


      1 #!/usr/bin/env sh
      2 
      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.
      6 #
      7 # Dependencies: lsblk, pmount
      8 #
      9 # Usage: Runs `lsblk` on 'l', exits on 'Return`.
     10 #
     11 # Notes:
     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 alterntive to power-down disks.
     17 #
     18 # Shell: POSIX compliant
     19 # Author: Arun Prakash Jana
     20 
     21 prompt="device name [e.g. sdXn] ('l'ist, 'q'uit): "
     22 
     23 lsblk
     24 
     25 printf "\nEnsure you aren't still in the mounted device.\n"
     26 printf "%s" "$prompt"
     27 read -r dev
     28 
     29 while [ -n "$dev" ]
     30 do
     31     if [ "$dev" = "l" ]; then
     32         lsblk
     33     elif [ "$dev" = "q" ]; then
     34         exit
     35     else
     36         if grep -qs "$dev " /proc/mounts; then
     37             sync
     38             if pumount "$dev"
     39             then
     40                 echo "$dev" unmounted.
     41                 if udisksctl power-off -b /dev/"$dev"
     42                 then
     43                     echo "$dev" ejected.
     44                 fi
     45             fi
     46         else
     47             pmount "$dev"
     48             echo "$dev" mounted to "$(lsblk -n /dev/"$dev" | rev | cut -d' ' -f1 | rev)".
     49         fi
     50     fi
     51 
     52     echo
     53     printf "%s" "$prompt"
     54     read -r dev
     55 done