script executes without any errors but does doesnt show any dialog box . OS kali linux

rexkyojuro

New Member
Joined
Mar 2, 2024
Messages
1
Reaction score
0
Credits
34
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
}
 

Attachments

  • Screenshot from 2024-03-02 06-29-08.png
    Screenshot from 2024-03-02 06-29-08.png
    70.2 KB · Views: 53


Moving this to Command Line, for now, where scripts are dealt with - it may ultimately move to Kali Linux.

Chris Turner
wizardfromoz
 
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
}
You’ve set up a couple of variables and set up some functions in your script.
The reason you aren’t seeing any output is because you aren’t actually calling any of the functions in your script.

So perhaps the next step is to test your functions by calling them one at a time?
E.g. add the following lines at the end of your script.
Bash:
create_account
deposit_money
withdraw_money
view_accounts
view_account_history
That will call each of the functions in turn.
That way you can test each function and ensure it’s working the way you want it to.

Then perhaps create a menu-system prompting the user to select an option and then call the appropriate function?!
 
Last edited:

Staff online


Top