#!/usr/bin/env sh ## **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 new_account() { clear read -p "Enter the name of your account: " user1 echo "" echo "Your account name will be $user1." echo "" read -n1 -p "Is that correct? [y/N] " yn case $yn in # If user selects yes. Insert their username and a timestamp into the database. [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_conn "select id from account where username='$user1';" | awk 'FNR == 3 {print}') # Gives the user the starting balance. $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_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}') echo "Account name: $acc_name" echo "Account ID: $acc_id" ;; # If the user selects anything other than [Yy] * ) echo "Aborted" ;; esac } list_user() { echo "" $db_conn "select * from account;" echo "" } list_balance() { # Bal Menu clear while true do echo "" echo "1) List all users balances." echo "2) Exact User Search" echo "3) User Search" echo "0) Go back." echo "" read -n1 -p "*Balance Menu: Select from the above: " ii echo "" case $ii in 1 ) clear $db_conn "select id,username,balance from account join balance using (id);" ;; 2 ) read -p "Enter the user account name: " 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_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 "" ;; [0q] ) break ;; esac done } bank_menu() { clear while true do echo "" echo "1) Withdraw" echo "2) Deposit" echo "3) Transfer" echo "4) Quick balance check" echo "" echo "0) Go Back" echo "" read -n1 -p "*Banking Menu: Select from the above: " ii_bank echo "" case $ii_bank in [0q] ) break ;; 1 ) read -p "Withdraw from user: " with_user read -p "Amount to withdraw: " with_amt LAST_USER=$with_user ## Not sure why this was here... #echo "$with_user" | awk 'FNR == 1 {print}' # Grabs the current amount the user has stored in the database. with_1_amt=$($db_conn "select id,username,balance from account join balance using (id) where username='$with_user';"| awk 'FNR == 3' | awk -F\| '{print $3}') # This grabs the user's ID to use on the balance table. with_user_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$with_user';" | awk 'FNR == 3' | awk -F\| '{print $1}') # Does the math to minus the withdraw. with_new_amt=$($db_conn "select balance - '$with_amt' from balance where id = '$with_user_id';" | awk 'FNR ==3') # Updates the database with the new amount. $db_conn "update balance set balance = '$with_new_amt' where id = '$with_user_id';" ;; 2 ) read -p "Deposit to user: " depo_user read -p "Amount to deposit: " depo_amt LAST_USER=$depo_user depo_1_amt=$($db_conn "select id,username,balance from account join balance using (id) where username='$depo_user';"| awk 'FNR == 3' | awk -F\| '{print $3}') # This grabs the user's ID to use on the balance table. depo_user_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$depo_user';" | awk 'FNR == 3' | awk -F\| '{print $1}') # Does the math to add the withdraw. depo_new_amt=$($db_conn "select balance + '$depo_amt' from balance where id = '$depo_user_id';" | awk 'FNR ==3') # Updates the database with the new amount. $db_conn "update balance set balance = '$depo_new_amt' where id = '$depo_user_id';" ;; 4 ) $db_conn "select id,username,balance from account join balance using (id) where username='$LAST_USER';" ;; * ) clear ;; esac done } clear while true do # Main Menu echo "" echo "DDBank Main Menu: " echo "1) Create new user account." echo "2) List user accounts." echo "3) List user(s) balance." echo "4) Banking (Deposit, withdraw, transfer)" echo "9) Clear contents of screen" echo "0) Exit" echo "" read -p "Select from the menu above: " -n1 i case $i in 1 ) new_account ;; 2 ) list_user ;; 3 ) list_balance ;; 4 ) bank_menu ;; 9 ) clear ;; 0 ) break ;; * ) clear ;; esac done #if [ $i == 1 ]; then { # new_account #} else { # echo "Some other thing..." #} fi