Code improvements

master
Matt Theissen 4 years ago
parent 1cae47d9f8
commit ed42a9a4f6

@ -12,19 +12,21 @@ ssh_host="knd-db01.kniod.corp"
# Functions # Functions
new_account() { new_account() {
read -p "Enter the name of your account: " user1 read -p "Enter the name of your account: " user1
echo ""
echo "Your account name will be $user1." echo "Your account name will be $user1."
read -p "Is that correct? [y/N] " yn echo ""
read -n1 -p "Is that correct? [y/N] " yn
case $yn in case $yn in
# If user selects yes. Insert their username and a timestamp into the database. # 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."; [Yy] ) ssh $ssh_user@$ssh_host "psql -U ddbank -d ddbank -c \"insert into account (username,timestamp) values ('$user1','now');\"" &>/dev/null || echo "Username already taken.";
# Sets the user's id as the variable. # 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}') 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. # 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);\"" ssh $ssh_user@$ssh_host "psql -U ddbank -d ddbank -c \"insert into balance (id,balance) values ('$user1_id',$bal);\"" &>/dev/null
# Pulls the user's account and ID for awk'ing # 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) user_account=$(ssh $ssh_user@$ssh_host "psql -U ddbank -d ddbank -c \"select id,username from account;\"" | grep $user1_id)
@ -36,8 +38,8 @@ new_account() {
;; ;;
# If user selects no # If the user selects anything other than [Yy]
[Nn]* ) echo "Aborted" * ) echo "Aborted"
;; ;;
esac esac
@ -53,9 +55,8 @@ echo "2) List user accounts."
echo "3) List user(s) balance." echo "3) List user(s) balance."
echo " " echo " "
read -p "Select from the menu above: " i read -p "Select from the menu above: " i
if [ $i == 1 ] if [ $i == 1 ]; then {
then
new_account new_account
else } else {
echo "Some other thing..." echo "Some other thing..."
fi } fi