#!/usr/bin/env sh #Config ## Starting balance bal="43.00" ## SSH Config ssh_user="theissenm" ssh_host="knd-db01.kniod.corp" # 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] ) 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. 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);\"" &>/dev/null # 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 the user selects anything other than [Yy] * ) echo "Aborted" ;; esac } list_user() { ssh $ssh_user@$ssh_host "psql -U ddbank -d ddbank -c \"select * from account;\"" } list_balance() { # Bal Menu clear while true do echo "1) List all users balances." echo "2) User Search" echo "3) Go back." echo "" read -n1 -p "*Balance Menu: Select from the above: " ii echo "" case $ii in 1 ) ssh $ssh_user@$ssh_host "psql -U ddbank -d ddbank -c \"select id,username,balance from account join balance using (id);\"" ;; 2 ) read -p "Enter the user account name: " bal_user bal_user1=$(ssh $ssh_user@$ssh_host "psql -U ddbank -d ddbank -c \"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 "" ;; 3 ) break ;; esac done } dwt_menu() { echo "sub-menu goes here" } while true do # Menu echo "" echo "DDBank Menu: " echo "1) Create new user account." echo "2) List user accounts." echo "3) List user(s) balance." echo "4) 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 ;; 9 ) clear ;; 0 ) break esac done #if [ $i == 1 ]; then { # new_account #} else { # echo "Some other thing..." #} fi