I learn BASH scripting occasionally from time to time, now I am working on a simple bash script that serves to maintain dove-cot user database. The general purpose of the script is able to list, add or delete user entries in the specified file. Please add some comments about the code - common mistake and etc... I want to learn more about code simplicity and avoiding bad habits.
#!/bin/bash
USERDB="/etc/dovecot/users"
MAILBOXMAP="/etc/postfix/virtual/virtual-mailbox-maps.cf"
## Add user function
add_user () {
# Reading username
while [[ -z "$USERNAME" ]]; do
echo -n "Please enter username: "
read "USERNAME"
done
# Reading domain name
while [[ -z "$DOMAIN" ]]; do
echo -n "Please enter domain name: "
read DOMAIN
done
# Reading password and generating hash with doveadm pw
while [[ -z "$PASSWORD" ]]; do
echo -n "Please enter user password: "
read "PASSWORD"
done
if [[ -n "$PASSWORD" ]]; then
USERPASS="$(doveadm pw -p $PASSWORD -s SHA512-CRYPT)"
fi
# Adding provided credentials to Dovecot userdb
echo "Adding user credentials to userdb..."
echo "$USERNAME@$DOMAIN:$USERPASS:::" >> "$USERDB"
# Adding provided user@domain to postfix mailbox map
echo "Adding user credentials to postfix map..."
echo "$USERNAME@$DOMAIN $DOMAIN/$USERNAME" >> "$MAILBOXMAP"
# Hashing the map
if [[ -f "$MAILBOXMAP" ]]; then
postmap hash://"$MAILBOXMAP"
echo "Hashing map is done!"
else
echo "Postfix map not exist or have been moved to another directory!"
exit 1
fi
exit 0
}
## List all users function
list_users () {
if [[ -f "$USERDB" ]]; then
awk -F: '/./'' {print $1} ' "$USERDB"
else
echo "User database not exist or have been moved to another directory!"
exit 1
fi
}
## Delete user function
delete_user () {
if [[ -n "$DELUSER" ]]; then
sed -i "/^$DELUSER$/d" $USERDB $MAILBOXMAP && echo "User account have been deleted!"
else
echo "User not found!"
exit 1
fi
}
case "$1" in
add|-a)
add_user
;;
list|-l)
list_users
;;
delete|-d)
shift
deluser="$1"
delete_user
;;
*)
echo "Unknown command"
;;
esac