Initial Versions of Script

This commit is contained in:
Sangelo 2023-07-14 09:38:01 +02:00
parent f7e355b5a4
commit 97d1570f05
5 changed files with 85 additions and 3 deletions

View file

@ -1,3 +1,6 @@
# User Management Script for Bash # User Management Script for Bash
Simple user management script for Bash, to be able to learn how to use Bash. Simple user management script for Bash, to be able to learn how to use Bash.<br>
by Sangelo & LogolicusZ as a school project. by Sangelo as a school project.
Takes users from a MYSQL table and creates them on a Linux system.<br>
You can also delete and list all users.

0
database/delete.sql Normal file
View file

7
database/initial.sql Normal file
View file

@ -0,0 +1,7 @@
CREATE DATABASE if not exists usermgmt;
USE usermgmt;
CREATE TABLE users (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
PRIMARY KEY (id)
);

View file

@ -1,4 +1,8 @@
#!/bin/bash #!/bin/bash
WEB_IP=""
WEB_PORT=""
echo "Downloading the scripts..." echo "Downloading the scripts..."
wget "$1"/scripts . wget "$WEB_IP":"$WEB_PORT"/database/initial.sql
wget "$WEB_IP":"$WEB_PORT"/user-mgmt.sh

68
management/user-mgmt.sh Executable file
View file

@ -0,0 +1,68 @@
#!/bin/bash
# User Management Script, powered by an SQL database
# (c) Sangelo | Read Licenses for more info: https://gitpot.dev/sangelo/user-mgmt
# Variables
E_BADARGS=65
MYSQL=`which mysql`
MYSQL_USER="sangelo"
USAGE="
$0 -- user management script
Usage:
init
add {username} {name} {description}
delete {username}
list
"
# Check for correct number of arguments and action
if [ $# -lt 1 ]
then
echo "$USAGE"
exit $E_BADARGS
fi
# Switch between different actions
case "$1" in
"init")
Q1="CREATE TABLE IF NOT EXISTS usermgmt.users (username varchar(255), name varchar(255), description varchar(255));"
SQL="${Q1}"
;;
"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}"
;;
"delete")
if [ $# -ne 2 ]
then
echo "Error: Invalid action."
echo "$USAGE"
exit $E_BADARGS
fi
Q1="DELETE FROM usermgmt.users WHERE username = '$2';"
SQL="${Q1}"
;;
"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
;;
esac
$MYSQL -u "$MYSQL_USER" -p -e "$SQL"