From b299bdf4846ab4d963c6a07ee25c0550ba75422e Mon Sep 17 00:00:00 2001 From: Matthew Saunders Brown Date: Fri, 12 Feb 2021 09:47:26 -0800 Subject: [PATCH] rearranged getopts, added yesno function --- bin/vmail-aliases-add.sh | 30 +++++++++++------------ bin/vmail-aliases-del.sh | 26 ++++++++++---------- bin/vmail-aliases-get.sh | 51 ++++++++++++++++++++-------------------- bin/vmail-domains-mod.sh | 2 ++ bin/vmail-mboxes-add.sh | 42 ++++++++++++++++----------------- etc/vmail.conf | 38 ++++++++++++++++++++++++++++++ 6 files changed, 115 insertions(+), 74 deletions(-) diff --git a/bin/vmail-aliases-add.sh b/bin/vmail-aliases-add.sh index 7ac5140..badcb95 100755 --- a/bin/vmail-aliases-add.sh +++ b/bin/vmail-aliases-add.sh @@ -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 diff --git a/bin/vmail-aliases-del.sh b/bin/vmail-aliases-del.sh index 08b9311..69ffad4 100755 --- a/bin/vmail-aliases-del.sh +++ b/bin/vmail-aliases-del.sh @@ -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" diff --git a/bin/vmail-aliases-get.sh b/bin/vmail-aliases-get.sh index 7b57fac..9ccdd08 100755 --- a/bin/vmail-aliases-get.sh +++ b/bin/vmail-aliases-get.sh @@ -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 diff --git a/bin/vmail-domains-mod.sh b/bin/vmail-domains-mod.sh index 401006c..074028b 100755 --- a/bin/vmail-domains-mod.sh +++ b/bin/vmail-domains-mod.sh @@ -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 diff --git a/bin/vmail-mboxes-add.sh b/bin/vmail-mboxes-add.sh index 538ae38..af6e807 100755 --- a/bin/vmail-mboxes-add.sh +++ b/bin/vmail-mboxes-add.sh @@ -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 diff --git a/etc/vmail.conf b/etc/vmail.conf index 14abfaa..745d9da 100644 --- a/etc/vmail.conf +++ b/etc/vmail.conf @@ -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 +}