bin/moul: improve reliability & add rough autocompletion

This commit is contained in:
xeruf 2022-02-12 09:36:09 +01:00
parent c79e9e3e68
commit d486261ea8
2 changed files with 21 additions and 13 deletions

View File

@ -0,0 +1,3 @@
#compdef moul
compadd -V unsorted $(lsblk --list --noheadings --output name)
# TODO lsblk --list --noheadings --output name,label,fstype | grep ".\+ [^ ]\+" | sed "s/ [^ ]*$//"

View File

@ -1,25 +1,30 @@
#!/bin/sh -e #!/bin/sh -e
# Mount a partition by label or device identifier automatically # Mount a partition by label or device identifier automatically
# TODO Autocomplete: lsblk --output label --noheadings _help="$0 <device> [mountpoint] [options...]"
arg=$1 case $1 in
test "$arg" = "--help" && echo "$0 <device> [mountpoint] [options...]" && exit 0 ("") ds && echo "$_help" && exit 2;;
if test "$arg" = "-u" ("--help") echo "$_help" && exit 0;;
then ("-u") shift
mountpoint="$2" for last; do true; done
sudo umount --verbose "$mountpoint" sudo umount --verbose "$@"
code=$? code=$?
sudo rm -df "$mountpoint" sudo rm -df "$last"
exit $code exit $code;;
fi esac
if grep -e "^LABEL=$arg[^\w/]" /etc/fstab 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 then # have to mount twice as the first one might be creating the directory
mount -L "$@" 2>/dev/null || mount -L "$@" mount -L "$@" 2>/dev/null || mount -L "$@"
exit $? exit $?
fi fi
if grep -e "[^\w=/]$arg[^\w/]" /etc/fstab # FSTAB: BY MOUNTPOINT/NAME
if grep --word-regexp "$arg" /etc/fstab
then mount "$@"; exit $? then mount "$@"; exit $?
fi fi
# MANUALLY
mountpoint="${2:-${MNT:-${XDG_RUNTIME_DIR}/mnt}/$(basename "$arg")}" mountpoint="${2:-${MNT:-${XDG_RUNTIME_DIR}/mnt}/$(basename "$arg")}"
if grep -e "[^\w=/]$mountpoint[^\w/]" /etc/fstab if grep -e "[^\w=/]$mountpoint[^\w/]" /etc/fstab
then shift; mount "$mountpoint" "$@"; code=$?; cd $mountpoint; exit $code then shift; mount "$mountpoint" "$@"; code=$?; cd $mountpoint; exit $code