rearranged getopts, added yesno function

This commit is contained in:
Matthew Saunders Brown 2021-02-12 09:47:26 -08:00
parent 5ad67c9e08
commit b299bdf484
6 changed files with 115 additions and 74 deletions

View File

@ -23,21 +23,6 @@ help()
exit
}
# set any options that were passed
while getopts "h" opt; do
case "${opt}" in
h )
help
exit;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit;;
esac
done
# check for and set alias & email address & split in to local part & domain
if [ -n "$2" ]; then
if [ $1 == "-h" ] || [ $2 == "-h" ]; then
@ -69,6 +54,21 @@ else
help
fi
# set any options that were passed
while getopts "h" opt; do
case "${opt}" in
h )
help
exit;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit;;
esac
done
# get domain_id (and thus check if domain already exists)
domain_id=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT id from vm_domains WHERE domain='$domain';"`
if [ -z $domain_id ] ; then

View File

@ -21,19 +21,6 @@ help()
exit
}
# set any options that were passed
while getopts "h" opt; do
case "${opt}" in
h )
help
exit;;
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
esac
done
# check for and set alias search term(s)
if [ -n "$1" ]; then
if [ $1 == "-h" ]; then
@ -55,6 +42,19 @@ if [ -n "$1" ]; then
fi
fi
# set any options that were passed
while getopts "h" opt; do
case "${opt}" in
h )
help
exit;;
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
esac
done
# build query
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-s -r -N -e"

View File

@ -27,31 +27,6 @@ help()
exit
}
# set any options that were passed
while getopts "achs" opt; do
case "${opt}" in
a )
aliassearch=true
;;
c )
cvs="| sed 's/\t/,/g'"
;;
h )
help
exit;;
s )
dbcmdopts="-s -N $dbcmdopts"
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
esac
done
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-e"
dbquery="SELECT vm_aliases.alias, vm_mboxes.mbox, vm_domains.domain FROM vm_aliases, vm_mboxes, vm_domains WHERE vm_aliases.mbox_id = vm_mboxes.id AND vm_mboxes.domain_id = vm_domains.id"
# check for and set alias search term(s)
if [ -n "$1" ]; then
if [ $1 == "-h" ]; then
@ -75,6 +50,32 @@ if [ -n "$1" ]; then
fi
fi
# set initial db query data
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-e"
dbquery="SELECT vm_aliases.alias, vm_mboxes.mbox, vm_domains.domain FROM vm_aliases, vm_mboxes, vm_domains WHERE vm_aliases.mbox_id = vm_mboxes.id AND vm_mboxes.domain_id = vm_domains.id"
# set any options that were passed
while getopts "achs" opt; do
case "${opt}" in
a )
aliassearch=true
;;
c )
cvs="| sed 's/\t/,/g'"
;;
h )
help
exit;;
s )
dbcmdopts="-s -N $dbcmdopts"
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
esac
done
# build query
if [ -n "$domain" ]; then
# add specific domain

View File

@ -26,6 +26,8 @@ help()
exit
}
# set & check $domain here
# set any options that were passed
while getopts ":s:l:q:" opt; do
case "${opt}" in

View File

@ -21,27 +21,6 @@ help()
exit
}
# set any options that were passed
while getopts "hq:s:" opt; do
case "${opt}" in
h )
help
exit;;
q )
quota=${OPTARG}
;;
s )
status=${OPTARG}
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit;;
esac
done
# check for and set email address, local part & domain, password
if [ -n "$2" ]; then
if [ $1 == "-h" ] || [ $2 == "-h" ]; then
@ -73,6 +52,27 @@ else
help
fi
# set any options that were passed
while getopts "hq:s:" opt; do
case "${opt}" in
h )
help
exit;;
q )
quota=${OPTARG}
;;
s )
status=${OPTARG}
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit;;
esac
done
# get domain_id (and thus check if domain already exists)
domain_id=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT id from vm_domains WHERE domain='$domain';"`
if [ -z $domain_id ] ; then

View File

@ -18,3 +18,41 @@ function vmail::validate_domain () {
fi
}
# yesno prompt
#
# Examples:
# loop until y or n: if vmail::yesno "Continue?"; then
# default y: if vmail::yesno "Continue?" Y; then
# default n: if vmail::yesno "Continue?" N; then
function vmail::yesno() {
local prompt default reply
if [ "${2:-}" = "Y" ]; then
prompt="Y/n"
default=Y
elif [ "${2:-}" = "N" ]; then
prompt="y/N"
default=N
else
prompt="y/n"
default=
fi
while true; do
read -p "$1 [$prompt] " -n 1 -r reply
# Default?
if [ -z "$reply" ]; then
reply=$default
fi
# Check if the reply is valid
case "$reply" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}