From 484f1e0a47f3c6a84aa028981cecc46c94109bda Mon Sep 17 00:00:00 2001 From: Matt Theissen Date: Sat, 17 Jul 2021 21:51:05 -0500 Subject: [PATCH] Add function for creating a user --- ddbank.sh | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 ddbank.sh diff --git a/ddbank.sh b/ddbank.sh new file mode 100755 index 0000000..47d1c0c --- /dev/null +++ b/ddbank.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env sh + +#Config +## Starting balance +bal="43.00" +## SSH Config +ssh_user="theissenm" +ssh_host="knd-db01.kniod.corp + + + +# Functions +new_account() { + read -p "Enter the name of your account: " user1 + echo "Your account name will be $user1." + read -p "Is that correct? [y/N]" yn + + case $yn in + + # If user selects yes. Insert their username and a timestamp into the database. + [Yy]* ) ssh $ssh_user$ssh_host "psql -U ddbank -d ddbank -c \"insert into account (username,timestamp) values ('$user1','now');\"" 2>/dev/null || echo "Username already taken."; + + # Sets the user's id as the variable. + user1_id=$(ssh $ssh_user@$ssh_host "psql -U ddbank -d ddbank -c \"select id from account where username='$user1';\"" | awk 'FNR == 3 {print}') + + # Gives the user the starting balance. + ssh $ssh_user@$ssh_host "psql -U ddbank -d ddbank -c \"insert into balance (id,balance) values ('$user1_id',$bal);\"" + + # Pulls the user's account and ID for awk'ing + user_account=$(ssh $ssh_user@$ssh_host "psql -U ddbank -d ddbank -c \"select id,username from account;\"" | grep $user1_id) + echo "" + acc_id=$(echo "$user_account" | awk -F\| '{print $1}') + acc_name=$(echo "$user_account" | awk -F\| '{print $2}') + echo "Account name: $acc_name" + echo "Account ID: $acc_id" + ;; + + + # If user selects no + [Nn]* ) echo "Aborted" + ;; + + esac + +} + + + + +# Menu +echo "This is the first option." +echo "This is the second" +echo "This is the third" +echo " " +read -p "What is your option?: " i +if [ $i == 1 ] +then + new_account +else + echo "Some other thing..." +fi