add usage, improve getopt

Signed-off-by: Bogomil Vasilev <smirky@smirky.net>
This commit is contained in:
2022-02-11 16:33:42 +02:00
parent 37e29989fe
commit 481fa5886a

View File

@@ -1,32 +1,52 @@
#!/bin/bash
#
# This script is indended to decrypt, mount and chroot in a LUKS+EFI+LVM USB
if [[ $UID != 0 ]]; then
printf "You need root permissions to use this script!\n"
exit 1
fi
usage()
{
echo -e "\nUsage: $(basename "$0") [options] [luks_device]\n\n" \
"Options:\n" \
"\t-m, --mount Mount the LUKS device (Default)\n" \
"\t-u, --umount Unmount the LUKS device\n" \
"\t-n, --nochroot Don't run arch-chroot\n" \
"\t-c, --chroot Run arch-chroot (Default)\n" \
"\t-h, --help Display this message\n"
}
# Mute lvm utilities
export LVM_SUPPRESS_FD_WARNINGS=true
# Some sane defaults
ACTION=mount
RUN_CHROOT=true
USB_LUKS_PART=
# Call getopt to validate the provided input.
options=$(getopt -o cmnu -l chroot,nochroot,mount,umount -- "$@")
if ! options=$(getopt -o chmnu -l chroot,nochroot,mount,umount,help -- "$@"); then
usage
exit 1
fi
eval set -- "$options"
while [ $# -gt 0 ]; do
while true; do
case "$1" in
(-m|--mount) ACTION=mount ;;
(-u|--umount) ACTION=umount ;;
(-n|--nochroot) RUN_CHROOT=false ;;
(-c|--chroot) RUN_CHROOT=true ;;
(-h|--help) usage; exit 1;;
(--) shift; break;;
(-*) echo "invalid option: $1" 1>&2; exit 1;;
(*) break;;
(*) usage; break;;
esac
shift
done
if [ "$#" -gt 1 ]; then
echo "Only 1 additional argument possible! Got $#: $*"
echo "error: Only 1 device argument possible! Got $#: $*"
usage
exit 1
fi