./install.sh -yq
but it doesn't work and I don't know where the problem is. It asks for confirmation even when I pass the argument. This is part of a post-install-script that I'm writing for Linux Minx.Code: Select all
# http://stackoverflow.com/questions/43364510/prompt-to-continue-if-no-argument-passed
getopt --test > /dev/null
if [[ $? != 4 ]]; then
echo "I’m sorry, $(getopt --test) failed in this environment."
exit 1
fi
SHORT=yq
LONG=yes,quick
# -temporarily store output to be able to check for errors
# -activate advanced mode getopt quoting e.g. via “--options”
# -pass arguments only via -- "$@" to separate them correctly
PARSED=$(getopt --options $SHORT --longoptions $LONG --name "$0" -- "$@")
if [[ $? != 0 ]]; then
# e.g. $? == 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
# use eval with "$PARSED" to properly handle the quoting
eval set -- "$PARSED"
h=0; q=0; v=0; autoConfirm=0
# now enjoy the options in order and nicely split until we see --
while true; do
case "$1" in
-h|--help)
h=1
shift
exit 2 # Use a dedicated exit code to signal this condition.
;;
-c|--config)
config=1
shift
;;
-v|--verbose)
v=1
shift
;;
-y|--yes)
autoConfirm=1
shift
;;
--)
shift
break
;;
*)
echo "Invalid option. Use -h for help"
exit 3
;;
esac
done
assertConfirmation () {
local promptMsg=$1 autoConfirm=$2
if (( autoConfirm )); then
return
else
clear
read -n 1 -p "$promptMsg (yes/No) "
printf '\n' # Output a newline, because none was appended to the user's keypress.
echo "========================================================================"
if [[ $REPLY =~ ^([Yy])$ ]]; then
return
fi
fi
return 1
}
Code: Select all
source install/essential "${autoConfirm:?}"