diff --git a/bin/vmail-domains-add.sh b/bin/vmail-domains-add.sh index aa30f5c..41a5ba1 100755 --- a/bin/vmail-domains-add.sh +++ b/bin/vmail-domains-add.sh @@ -64,6 +64,11 @@ while getopts "hl:q:s:" opt; do esac done +# set initial db query data +dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE" +dbcmdopts=" -s -r -N -e" +dbquery="SELECT * from vm_domains" + # check if vmail domain dir exits if [ -d $VMAIL_DIR/$domain ]; then echo "ERROR: Directory $VMAIL_DIR/$domain already exists." @@ -71,13 +76,15 @@ if [ -d $VMAIL_DIR/$domain ]; then fi # check if domain exists in vmail database -rowcount=`mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -s -r -N -e "SELECT COUNT(*) from vm_domains WHERE domain='$domain';"` +dbquery="SELECT COUNT(*) from vm_domains WHERE domain='$domain';"; +rowcount=`$dbcmd $dbcmdopts "$dbquery"` if [ "$rowcount" -eq '0' ] ; then # looks good, build SQL + dbquery="INSERT INTO vm_domains SET domain='$domain'"; cmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e \"INSERT INTO vm_domains SET domain='$domain'\"" if [ ! -z "$status" ] ; then if [ "$status" == 0 ] || [ "$status" == 1 ]; then - cmd="$cmd, status='$status'" + dbquery="$dbquery, status='$status'" else echo "ERROR: status (-s) must be 1 or 0" exit 1 @@ -85,9 +92,9 @@ if [ "$rowcount" -eq '0' ] ; then fi if [ ! -z "$limit" ] ; then if [[ "$limit" == "NULL" ]]; then - cmd="$cmd, mbox_limit=NULL" + dbquery="$dbquery, mbox_limit=NULL" elif [[ "$limit" =~ ^[0-9]+$ ]]; then - cmd="$cmd, mbox_limit='$limit'" + dbquery="$dbquery, mbox_limit='$limit'" else echo "ERROR: limit (-l) must numeric or NULL" exit 1 @@ -95,23 +102,23 @@ if [ "$rowcount" -eq '0' ] ; then fi if [ ! -z "$quota" ] ; then if [[ "$quota" == "NULL" ]]; then - cmd="$cmd, mbox_quota_default=NULL" + dbquery="$dbquery, mbox_quota_default=NULL" elif [[ "$quota" =~ ^[0-9]+$ ]]; then - cmd="$cmd, mbox_quota_default='$quota'" + dbquery="$dbquery, mbox_quota_default='$quota'" else echo "ERROR: quota (-q) must numeric or NULL" exit 1 fi fi - cmd="$cmd;" + dbquery="$dbquery;" # add domain to vmail database - echo $cmd + eval $dbcmd $dbcmdopts "\"$dbquery\"" # create vmail directory for domain if [ ! -d "$VMAIL_DIR" ] ; then - echo "install -o vmail -g vmail -m 750 -d $VMAIL_DIR" + install -o vmail -g vmail -m 750 -d $VMAIL_DIR fi if [ ! -d "$VMAIL_DIR/$domain" ] ; then - echo "install -o vmail -g vmail -m 750 -d $VMAIL_DIR/$domain" + install -o vmail -g vmail -m 750 -d $VMAIL_DIR/$domain fi elif [ "$rowcount" -eq '1' ] ; then echo "ERROR: $domain already exists in vmail database."