From 97d1570f05f0a8b7069a65dbe16ac7206dc54160 Mon Sep 17 00:00:00 2001 From: Sangelo Date: Fri, 14 Jul 2023 09:38:01 +0200 Subject: [PATCH] Initial Versions of Script --- README.md | 7 +++-- database/delete.sql | 0 database/initial.sql | 7 +++++ download-script.sh | 6 +++- management/user-mgmt.sh | 68 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 database/delete.sql create mode 100644 database/initial.sql create mode 100755 management/user-mgmt.sh diff --git a/README.md b/README.md index 50c850b..676cfbe 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ # User Management Script for Bash -Simple user management script for Bash, to be able to learn how to use Bash. -by Sangelo & LogolicusZ as a school project. \ No newline at end of file +Simple user management script for Bash, to be able to learn how to use Bash.
+by Sangelo as a school project. + +Takes users from a MYSQL table and creates them on a Linux system.
+You can also delete and list all users. \ No newline at end of file diff --git a/database/delete.sql b/database/delete.sql new file mode 100644 index 0000000..e69de29 diff --git a/database/initial.sql b/database/initial.sql new file mode 100644 index 0000000..0a028b6 --- /dev/null +++ b/database/initial.sql @@ -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) +); \ No newline at end of file diff --git a/download-script.sh b/download-script.sh index 8c14caa..eef9df0 100755 --- a/download-script.sh +++ b/download-script.sh @@ -1,4 +1,8 @@ #!/bin/bash +WEB_IP="" +WEB_PORT="" + echo "Downloading the scripts..." -wget "$1"/scripts . \ No newline at end of file +wget "$WEB_IP":"$WEB_PORT"/database/initial.sql +wget "$WEB_IP":"$WEB_PORT"/user-mgmt.sh \ No newline at end of file diff --git a/management/user-mgmt.sh b/management/user-mgmt.sh new file mode 100755 index 0000000..da6a78d --- /dev/null +++ b/management/user-mgmt.sh @@ -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"