Skip to content

Instantly share code, notes, and snippets.

@nobrowser
Created January 25, 2017 23:30
Show Gist options
  • Save nobrowser/e2c8925255720635f15a135856d4bcbb to your computer and use it in GitHub Desktop.
Save nobrowser/e2c8925255720635f15a135856d4bcbb to your computer and use it in GitHub Desktop.
Mount removable drives in a quick and flexible way from the command line
#! /bin/sh -Cefu
. /usr/local/bin/fortify.sh
. /usr/local/bin/mashlib.sh
MODE=name
usage()
{
echo 'usage: pmtw ( NAME | -u UUID | -l LABEL | -p PARTITION )' >&2
exit 2
}
rc_lookup()
{
grep -v -e '^ *$' -e '^ *#' $(conc $HOME /.pmtwrc) | \
awk -v name=${NAME} '$1 == name {print $2 " " $3 " " $4}' | \
head -n 1
}
while getopts :ulp opt ; do
case $opt in
(u)
MODE=uuid
break
;;
(l)
MODE=label
break
;;
(p)
MODE=partition
break
;;
('?')
usage
;;
esac
done
shift $(( OPTIND - 1 ))
case $# in
(1)
true
;;
(*)
usage
;;
esac
NAME=$1
case $MODE in
(partition)
pmount $(conc /dev/ $NAME) $NAME
;;
(uuid)
pmount $(conc /dev/disk/by-uuid/ $NAME) $NAME
;;
(label)
pmount $(conc /dev/disk/by-label/ $NAME) $NAME
;;
(name)
ifson ; set -- $(rc_lookup) ; ifsoff
case $# in
(3)
true
;;
(*)
echo "${NAME} not found, or bad config" >&2
exit 1
;;
esac
pmount -t $3 $(conc /dev/disk/by- $1 / $2) $NAME
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment