dotfiles/.local/bin/scripts/moul

45 lines
1.2 KiB
Bash
Executable File

#!/bin/sh -e
# Mount a partition by label or device identifier automatically
_help="$0 <device> [mountpoint] [options...]"
case $1 in
("") ds && echo "$_help" && exit 2;;
("--help") echo "$_help" && exit 0;;
("-u") shift
for last; do true; done
sudo umount --verbose "$@"
code=$?
sudo rm -df "$last"
exit $code;;
esac
arg=$1
# FSTAB: BY LABEL
if grep --word-regexp "LABEL=$arg" /etc/fstab
then # have to mount twice as the first one might be creating the directory
mount -L "$@" 2>/dev/null || mount -L "$@"
exit $?
fi
# FSTAB: BY MOUNTPOINT/NAME
if grep --word-regexp "$arg" /etc/fstab
then mount "$@"; exit $?
fi
# MANUALLY
mountpoint="${2:-${MNT:-${XDG_RUNTIME_DIR}/mnt}/$(basename "$arg")}"
if grep -e "[^\w=/]$mountpoint[^\w/]" /etc/fstab
then shift; mount "$mountpoint" "$@"; code=$?; cd $mountpoint; exit $code
fi
case "$arg" in
(sd*|loop*|nvme*|mm*|md*|dm*|vg*) partition="/dev/$arg";;
(/dev/*) partition="$arg";;
(*) partition="-L $arg";;
esac
shift $(expr 2 \& $# \> 1 \| 1)
#uid=$(id --user),gid=$(id --group), \
if ! mountpoint "$mountpoint" 2>/dev/null
then mp="/run/media/$USER/$arg" && test -e "$mp" && mountpoint="$mp"
sudo mount -vo users,X-mount.mkdir,noatime $partition $mountpoint "$@"
fi
cd $mountpoint
exec $SHELL