From a4011543565cb769d82b5ef3ac9bda7a5298f14c Mon Sep 17 00:00:00 2001 From: Raymaekers Luca Date: Wed, 19 Jul 2023 11:35:58 +0200 Subject: replaced amount with aumount --- bin/common/aumount | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100755 bin/common/aumount (limited to 'bin/common/aumount') diff --git a/bin/common/aumount b/bin/common/aumount new file mode 100755 index 0000000..790106d --- /dev/null +++ b/bin/common/aumount @@ -0,0 +1,90 @@ +#!/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 +} + +# eject the device with $1 as the choice +eject () +{ + printf "$1" | grep -q "[0-9]" || exit 1 + mountpoint="$(sed -n "${1}p" "$tmp" | + awk '{print $3}')" + test -z "$mountpoint" && exit 1 + + # Get device for mountpoint with df + dev="$(df -P "$mountpoint" | + tail -n +2 | head -n 1 | # skip headers + awk '{print $1}')" + + die "ejecting $dev" + $sudo eject "$dev" && + die "Succesfully ejected." || + die "Failed to eject." +} + +# 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) e(ject) ?" +choice="$(read_char)" + +case $choice in + "m") i=2; cmd=mount ;; + "e") i=1; cmd=eject ;; + *) exit 1 ;; +esac + +pr_lsblk $i +choice="$(read_char)" +$cmd $choice -- cgit v1.2.3 From 4b20907a634a47e578d97df24cd7b632c1c06786 Mon Sep 17 00:00:00 2001 From: Raymaekers Luca Date: Wed, 19 Jul 2023 11:41:43 +0200 Subject: umount instead of eject --- bin/common/aumount | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'bin/common/aumount') diff --git a/bin/common/aumount b/bin/common/aumount index 790106d..f7f1ecb 100755 --- a/bin/common/aumount +++ b/bin/common/aumount @@ -25,29 +25,23 @@ mount () dev="$(grep "^$1\." "$tmp" | cut -f 2- -d ' ')" test -z "$dev" && exit - die "mounting /dev/$dev on /media/$dev" + die "Mounting /dev/$dev on /media/$dev" mkdir -p /media/$dev $sudo mount /dev/$dev /media/$dev > /dev/null 2>&1 || return 1 } -# eject the device with $1 as the choice -eject () +# 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 - # Get device for mountpoint with df - dev="$(df -P "$mountpoint" | - tail -n +2 | head -n 1 | # skip headers - awk '{print $1}')" - - die "ejecting $dev" - $sudo eject "$dev" && - die "Succesfully ejected." || - die "Failed to eject." + die "Unmounting $mountpoint" + $sudo umount "$mountpoint" || + return 1 } # print lsblk, use $1 to print only devices with mountpoints or without @@ -76,15 +70,17 @@ cleanup () trap cleanup EXIT INT -die "m(ount) e(ject) ?" +die "m(ount) u(mount) ?" choice="$(read_char)" case $choice in "m") i=2; cmd=mount ;; - "e") i=1; cmd=eject ;; + "u") i=1; cmd=umount ;; *) exit 1 ;; esac pr_lsblk $i choice="$(read_char)" -$cmd $choice +$cmd $choice && + die "Successful." || + die "Failed." -- cgit v1.2.3 From 7533d26a043df25183a78eba5cce5ca5c3d0cbe6 Mon Sep 17 00:00:00 2001 From: Raymaekers Luca Date: Wed, 19 Jul 2023 14:46:46 +0200 Subject: added eject option to aumount --- bin/common/aumount | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'bin/common/aumount') diff --git a/bin/common/aumount b/bin/common/aumount index f7f1ecb..19b33e0 100755 --- a/bin/common/aumount +++ b/bin/common/aumount @@ -19,11 +19,16 @@ read_char () die "" } +get_dev () +{ + grep "^$1\." "$tmp" | cut -f 2- -d ' ' +} + # mount the device with $1 as the choice mount () { - dev="$(grep "^$1\." "$tmp" | cut -f 2- -d ' ')" - test -z "$dev" && exit + dev="$(get_dev "$1")" + test -z "$dev" && exit 1 die "Mounting /dev/$dev on /media/$dev" mkdir -p /media/$dev @@ -34,7 +39,6 @@ mount () # 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 @@ -44,6 +48,16 @@ umount () return 1 } +ejekt () +{ + dev="$(get_dev "$1" | sed 's/.$//')" + test -z "$dev" && exit 1 + + die "Ejecting /dev/$dev" + $sudo eject /dev/$dev > /dev/null 2>&1 || + return 1 +} + # print lsblk, use $1 to print only devices with mountpoints or without pr_lsblk () { @@ -70,17 +84,19 @@ cleanup () trap cleanup EXIT INT -die "m(ount) u(mount) ?" +die "m(ount) u(mount) (e)ject ?" choice="$(read_char)" case $choice in "m") i=2; cmd=mount ;; "u") i=1; cmd=umount ;; + "e") i=2; cmd=ejekt ;; *) exit 1 ;; esac pr_lsblk $i choice="$(read_char)" +printf "$choice" | grep -q "[0-9]" || exit 1 $cmd $choice && die "Successful." || die "Failed." -- cgit v1.2.3