format script properly

This commit is contained in:
Sangelo 2023-07-14 10:22:49 +02:00
parent 758fab05df
commit 42b0869883

View file

@ -4,7 +4,7 @@
# Variables
E_BADARGS=65
MYSQL=`which mysql`
MYSQL=$(which mysql)
MYSQL_USER="root"
USAGE="
$0 -- user management script
@ -16,45 +16,41 @@ Usage:
"
# Check for correct number of arguments and action
if [ $# -lt 1 ]
then
echo "$USAGE"
exit $E_BADARGS
if [ $# -lt 1 ]; then
echo "$USAGE"
exit $E_BADARGS
fi
# Switch between different actions
case "$1" in
"init")
"init")
Q1="CREATE SCHEMA if not exists usermgmt; use usermgmt; CREATE TABLE if not exists users (username varchar(255), name varchar(255), description varchar(255));"
SQL="${Q1}"
;;
"add")
if [ $# -ne 4 ]
then
"add")
if [ $# -ne 4 ]; then
echo "Error: Invalid action."
echo "$USAGE"
exit $E_BADARGS
fi
Q1="INSERT INTO usermgmt.users (username, name, description) VALUES ('\$2', '\$3', '\$4');"
SQL="${Q1}"
PASSWORD=`openssl rand -base64 12`
PASSWORD=$(openssl rand -base64 12)
useradd -m -p "$PASSWORD" -s /bin/bash "\$2"
echo "$PASSWORD" > "/home/\$2/password.txt"
echo "This is your password. Please delete this file after saving the password." >> "/home/\$2/password.txt"
echo "$PASSWORD" >"/home/\$2/password.txt"
echo "This is your password. Please delete this file after saving the password." >>"/home/\$2/password.txt"
;;
"delete")
if [ $# -ne 2 ]
then
"delete")
if [ $# -ne 2 ]; then
echo "Error: Invalid action."
echo "$USAGE"
exit $E_BADARGS
fi
echo -n "Are you sure you want to delete user "$2"? [y/N] "
read confirm
if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]
then
if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
Q1="DELETE FROM usermgmt.users WHERE username = '$2';"
SQL="${Q1}"
userdel -r "$2"
@ -64,18 +60,17 @@ case "$1" in
fi
;;
"list")
if [ $# -ne 1 ]
then
echo "Error: Invalid action."
echo "$USAGE"
exit $E_BADARGS
"list")
if [ $# -ne 1 ]; then
echo "Error: Invalid action."
echo "$USAGE"
exit $E_BADARGS
fi
Q1="SELECT username, name, description FROM usermgmt.users;"
SQL="${Q1}"
;;
*)
*)
echo "Error: Invalid action. Please use a valid argument."
echo "$USAGE"
exit $E_BADARGS
@ -84,4 +79,4 @@ esac
echo "Creating MySQL connection... Enter your password if prompted."
$MYSQL -u "$MYSQL_USER" -p -e "$SQL"
echo "Done."
echo "Done."