rexkyojuro
New Member
i have run all the necessary comands . like installing dialog and chmod +x to give it write perms anything im doing wrong here pls help also this is my unix project submissions on monday
the code is
#!/bin/bash
# File to store account data
ACCOUNT_FILE="account_data.txt"
# File to store transaction history
TRANSACTION_FILE="transaction_history.txt"
# Function to create a new account
create_account() {
account_number=$(dialog --inputbox "Enter account number:" 8 40 --stdout)
account_holder=$(dialog --inputbox "Enter account holder's name:" 8 40 --stdout)
account_balance=$(dialog --inputbox "Enter initial account balance:" 8 40 --stdout)
echo "$account_number,$account_holder,$account_balance" >> "$ACCOUNT_FILE"
dialog --msgbox "Account created successfully." 8 40
}
# Function to deposit money
deposit_money() {
view_accounts
account_number=$(dialog --inputbox "Enter account number:" 8 40 --stdout)
amount=$(dialog --inputbox "Enter amount to deposit:" 8 40 --stdout)
if account_line=$(grep -m 1 "^$account_number," "$ACCOUNT_FILE"); then
account_balance=$(echo "$account_line" | cut -d ',' -f3)
new_balance=$(echo "$account_balance + $amount" | bc)
sed -i "s/^$account_number,[^,],[^,]/$account_number,$account_holder,$new_balance/" "$ACCOUNT_FILE"
echo "$(date "+%Y-%m-%d %H:%M:%S"),Deposit,$account_number,$amount,$new_balance" >> "$TRANSACTION_FILE"
dialog --msgbox "Deposit successful. New balance: $new_balance" 8 40
else
dialog --msgbox "Account not found." 8 40
fi
}
# Function to withdraw money
withdraw_money() {
view_accounts
account_number=$(dialog --inputbox "Enter account number:" 8 40 --stdout)
amount=$(dialog --inputbox "Enter amount to withdraw:" 8 40 --stdout)
if account_line=$(grep -m 1 "^$account_number," "$ACCOUNT_FILE"); then
account_balance=$(echo "$account_line" | cut -d ',' -f3)
if [ "$(echo "$account_balance >= $amount" | bc)" -eq 1 ]; then
new_balance=$(echo "$account_balance - $amount" | bc)
sed -i "s/^$account_number,[^,],[^,]/$account_number,$account_holder,$new_balance/" "$ACCOUNT_FILE"
echo "$(date "+%Y-%m-%d %H:%M:%S"),Withdrawal,$account_number,$amount,$new_balance" >> "$TRANSACTION_FILE"
dialog --msgbox "Withdrawal successful. New balance: $new_balance" 8 40
else
dialog --msgbox "Insufficient balance." 8 40
fi
else
dialog --msgbox "Account not found." 8 40
fi
}
# Function to view all accounts
view_accounts() {
if [ -s "$ACCOUNT_FILE" ]; then
dialog --title "Account List" --textbox "$ACCOUNT_FILE" 20 80
else
dialog --title "Account List" --msgbox "No accounts found." 8 40
fi
}
# Function to view transaction history
view_transaction_history() {
if [ -s "$TRANSACTION_FILE" ]; then
dialog --title "Transaction History" --textbox "$TRANSACTION_FILE" 20 80
else
dialog --title "Transaction History" --msgbox "No transactions found." 8 40
fi
}
the code is
#!/bin/bash
# File to store account data
ACCOUNT_FILE="account_data.txt"
# File to store transaction history
TRANSACTION_FILE="transaction_history.txt"
# Function to create a new account
create_account() {
account_number=$(dialog --inputbox "Enter account number:" 8 40 --stdout)
account_holder=$(dialog --inputbox "Enter account holder's name:" 8 40 --stdout)
account_balance=$(dialog --inputbox "Enter initial account balance:" 8 40 --stdout)
echo "$account_number,$account_holder,$account_balance" >> "$ACCOUNT_FILE"
dialog --msgbox "Account created successfully." 8 40
}
# Function to deposit money
deposit_money() {
view_accounts
account_number=$(dialog --inputbox "Enter account number:" 8 40 --stdout)
amount=$(dialog --inputbox "Enter amount to deposit:" 8 40 --stdout)
if account_line=$(grep -m 1 "^$account_number," "$ACCOUNT_FILE"); then
account_balance=$(echo "$account_line" | cut -d ',' -f3)
new_balance=$(echo "$account_balance + $amount" | bc)
sed -i "s/^$account_number,[^,],[^,]/$account_number,$account_holder,$new_balance/" "$ACCOUNT_FILE"
echo "$(date "+%Y-%m-%d %H:%M:%S"),Deposit,$account_number,$amount,$new_balance" >> "$TRANSACTION_FILE"
dialog --msgbox "Deposit successful. New balance: $new_balance" 8 40
else
dialog --msgbox "Account not found." 8 40
fi
}
# Function to withdraw money
withdraw_money() {
view_accounts
account_number=$(dialog --inputbox "Enter account number:" 8 40 --stdout)
amount=$(dialog --inputbox "Enter amount to withdraw:" 8 40 --stdout)
if account_line=$(grep -m 1 "^$account_number," "$ACCOUNT_FILE"); then
account_balance=$(echo "$account_line" | cut -d ',' -f3)
if [ "$(echo "$account_balance >= $amount" | bc)" -eq 1 ]; then
new_balance=$(echo "$account_balance - $amount" | bc)
sed -i "s/^$account_number,[^,],[^,]/$account_number,$account_holder,$new_balance/" "$ACCOUNT_FILE"
echo "$(date "+%Y-%m-%d %H:%M:%S"),Withdrawal,$account_number,$amount,$new_balance" >> "$TRANSACTION_FILE"
dialog --msgbox "Withdrawal successful. New balance: $new_balance" 8 40
else
dialog --msgbox "Insufficient balance." 8 40
fi
else
dialog --msgbox "Account not found." 8 40
fi
}
# Function to view all accounts
view_accounts() {
if [ -s "$ACCOUNT_FILE" ]; then
dialog --title "Account List" --textbox "$ACCOUNT_FILE" 20 80
else
dialog --title "Account List" --msgbox "No accounts found." 8 40
fi
}
# Function to view transaction history
view_transaction_history() {
if [ -s "$TRANSACTION_FILE" ]; then
dialog --title "Transaction History" --textbox "$TRANSACTION_FILE" 20 80
else
dialog --title "Transaction History" --msgbox "No transactions found." 8 40
fi
}