From 10dc3986dbcb1d428542c787edb0e54464f9f9ed Mon Sep 17 00:00:00 2001 From: Matt Theissen Date: Tue, 20 Jul 2021 23:13:40 -0500 Subject: [PATCH] Removed usage of SSH in favor of psql -h -p --- ddbank.sh | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/ddbank.sh b/ddbank.sh index f83bf3f..ff4349e 100755 --- a/ddbank.sh +++ b/ddbank.sh @@ -1,12 +1,23 @@ #!/usr/bin/env sh -#Config -## Starting balance -bal="00.00" -## SSH Config -ssh_user="theissenm" -ssh_host="knd-db01.kniod.corp" -db_ssh="ssh $ssh_user@$ssh_host" +## **Config file should have the following: +## #Config +## ## Starting balance +## bal="00.00" +## +## ## Postgres +## ### Database user +## dbuser="ddbank" +## ### Database Port +## dbport="5432" +## ### Datavase host +## dbhost="knd-db01.kniod.corp" +## ### Database name +## datab="ddbank" + +## db_conn="psql -h $dbhost -p $dbport -U $dbuser -d $datab -c" + +source $HOME/git/knd-ddbank/ddbank_config # Functions @@ -21,16 +32,16 @@ new_account() { case $yn in # If user selects yes. Insert their username and a timestamp into the database. - [Yy] ) $db_ssh "psql -U ddbank -d ddbank -c \"insert into account (username,timestamp) values ('$user1','now');\"" &>/dev/null || echo "Username already taken."; + [Yy] ) $db_conn "insert into account (username,timestamp) values ('$user1','now');" &>/dev/null || echo "Username already taken."; # Sets the user's id as the variable. - user1_id=$($db_ssh "psql -U ddbank -d ddbank -c \"select id from account where username='$user1';\"" | awk 'FNR == 3 {print}') + user1_id=$($db_conn "select id from account where username='$user1';" | awk 'FNR == 3 {print}') # Gives the user the starting balance. - $db_ssh "psql -U ddbank -d ddbank -c \"insert into balance (id,balance) values ('$user1_id',$bal);\"" &>/dev/null + $db_conn "insert into balance (id,balance) values ('$user1_id',$bal);" &>/dev/null # Pulls the user's account and ID for awk'ing - user_account=$($db_ssh "psql -U ddbank -d ddbank -c \"select id,username from account;\"" | grep $user1_id) + user_account=$($db_conn "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}') @@ -48,7 +59,7 @@ new_account() { list_user() { echo "" - $db_ssh "psql -U ddbank -d ddbank -c \"select * from account;\"" + $db_conn "select * from account;" echo "" } @@ -68,11 +79,11 @@ list_balance() { case $ii in 1 ) clear - $db_ssh "psql -U ddbank -d ddbank -c \"select id,username,balance from account join balance using (id);\"" ;; + $db_conn "select id,username,balance from account join balance using (id);" ;; 2 ) read -p "Enter the user account name: " ex_mat - $db_ssh "psql -U ddbank -d ddbank -c \"select id,username,balance from account join balance using (id) where username='$ex_mat';\"" ;; + $db_conn "select id,username,balance from account join balance using (id) where username='$ex_mat';" ;; 3 ) read -p "Enter search term: " bal_user - bal_user1=$($db_ssh "psql -U ddbank -d ddbank -c \"select id,username,balance from account join balance using (id);\"") + bal_user1=$($db_conn "select id,username,balance from account join balance using (id);") echo "$bal_user1" | awk 'FNR == 1 {print}' echo "$bal_user1" | grep -iE "$bal_user" echo "" ;; @@ -99,10 +110,10 @@ do 1 ) read -p "Withdraw from user: " bank_user read -p "Amount to withdraw: " bank_wth_amt echo "$bank_user" | awk 'FNR == 1 {print}' - bank_1_amt=$($db_ssh "psql -U ddbank -d ddbank -c \"select id,username,balance from account join balance using (id) where username='$bank_user';\""| awk 'FNR == 3' | awk -F\| '{print $3}') - bank_user_id=$($db_ssh "psql -U ddbank -d ddbank -c \"select id,username,balance from account join balance using (id) where username='$bank_user';\"" | awk 'FNR == 3' | awk -F\| '{print $1}') - new_amt=$($db_ssh "psql -U ddbank -d ddbank -c \"select balance - '$bank_wth_amt' from balance where id = '$bank_user_id';\"" | awk 'FNR ==3') - $db_ssh "psql -U ddbank -d ddbank -c \"update balance set balance = '$new_amt' where id = '$bank_user_id';\"" ;; + bank_1_amt=$($db_conn "select id,username,balance from account join balance using (id) where username='$bank_user';"| awk 'FNR == 3' | awk -F\| '{print $3}') + bank_user_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$bank_user';" | awk 'FNR == 3' | awk -F\| '{print $1}') + new_amt=$($db_conn "select balance - '$bank_wth_amt' from balance where id = '$bank_user_id';" | awk 'FNR ==3') + $db_conn "update balance set balance = '$new_amt' where id = '$bank_user_id';" ;; * ) clear ;; esac done