Case Contruct Problem user input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Case Contruct Problem user input
# 1  
Old 05-09-2012
Case Contruct Problem user input

This is supposed to be a simple bank script. Whenever I try the case construct options it wont work for the deposit option after typing the amount it just goes straight back into the menu, same with withdrawal option. Option 3 which should just display the balance amount doesn't echo anything. The exit option and the * option work fine. I've been debugging this for a few hours now and can't seem to find what it is. Can anyone here help me by pointing me in the right direction?

Thanks in advance.

Code:
# MENU
error_flag=0
balance=0
while true
do
if [ $error_flag -eq 0 ]
then
  tput clear
  echo "Welcome valued customer"
  echo "Please choose from the following options:"
  echo "1: Deposit "
  echo "2: Withdrawal "
  echo "3: Display balance "
  echo "4: Quit "
fi
error_flag=0
echo " Please enter your choice>......."
read choice
case $choice in
  1) printf "How much $ would you like to deposit?"
     read deposit
     ((balance = $deposit + $balance))
     printf "Your current balance is now $balance" ;;

  2) echo "How much $ would you like to withdraw?" ;
     read withdraw
     (( withdraw=$withdraw /- $balance ))
     echo "Your remaining balance is $balance dollars" ;;

  3) echo "Your current balance is $balance dollars" ;;
  4) echo "Thank you for banking with us"
     exit 0 ;;
  *) echo "Please only type the options given in the menu"
     error_flag=1 ;;
esac
export balance
export deposit
export withdraw
done
exit 0

# 2  
Old 05-09-2012
Try inserting either a sleep statement or a pause after your final statements within your case block. It's printing out the information, however you can not see it as it is completing the execution of the case block:

i.e.
Code:
case "$choice" in
  1) printf "How much $ would you like to deposit?"
     read deposit
     ((balance = $deposit + $balance))
     printf "Your current balance is now $balance"
     sleep 5
     ;;

Hope this helps.
# 3  
Old 05-09-2012
That worked perfectly

weird how something so simple, can cause such headaches to a beginner.

thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading user input...problem with tab key

Hi all, I have a little problem with my shell script (reading user input, save user input to variable, invisible characters in the log file :() printf "1. What's your file path?" /path/to/my/file read -e FILE I have invisible characters in my log file (e.g. <ESC> or ^G) when I'm... (3 Replies)
Discussion started by: splendid
3 Replies

2. Homework & Coursework Questions

How to read user keyboard input inside the case?

I need to Write a shell script that allows some system-administration tasks to be preformed automatically from a menu-driven interface. with automated following tasks: Copy directory tree Delete files or directories Output Information (this part is done ) *Copy directory tree The “Copy... (2 Replies)
Discussion started by: femchi
2 Replies

3. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

4. Shell Programming and Scripting

How to ignore case(upper and lower) from user input?

Hi, In my script it takes the input from the user. i.e. sys or system. How can i make it case insensitive in my code. if || ; then echo "valid" Any help is appreciated. Thanks, priya (4 Replies)
Discussion started by: priya001
4 Replies

5. Shell Programming and Scripting

Trouble in getting user input while using CASE statement in UNIX

i want to get user input like this please tell which option to chose 1. mango 2. tango 3. rango if user chooses mango then it should execute a set of statements and again ask like this what do you want to do 1.add 2.subtract 3.exit when i choose exit it should goto my previous menu... (4 Replies)
Discussion started by: s.deepak
4 Replies

6. Shell Programming and Scripting

!!VERY URGENT!! Trouble in getting user input, while using under CASE statement in UNIX

i want to get user input like this please tell which option to chose 1. mango 2. tango 3. rango if user chooses mango then it should execute a set of statements and again ask like this what do you want to do 1.add 2.subtract 3.exit when i choose exit it should goto my previous... (1 Reply)
Discussion started by: s.deepak
1 Replies

7. Shell Programming and Scripting

redirect input within case statement?

I'm trying to run the logic below but get a `<' is not matched error message when I return a Y or y; printf "Run this ? : " read RESP case $RESP in Y|y) cat <<EOF > file today is Monday EOF ;; N|n) exit 1 ;; esac Any ideas? (2 Replies)
Discussion started by: h8mmer
2 Replies

8. Shell Programming and Scripting

Accepting Input regardless of Case

Hi I am trying to get my script to accept input regardless if the person enters a or A. here is the portion of the code where I get the input. echo -n 'Please enter your choice:' # prompt user for input. read reply # read input echo case $reply in #... (2 Replies)
Discussion started by: DualPandas
2 Replies

9. Shell Programming and Scripting

To ignore user input case.

hi, i will like to know whether awk command can ignore case? i written a script that will take in user input and search for data on the 1st field from a text file. echo -n "Title:" read title awk -F":" '$1~/'"$title"'/{print $0}' Filename read ans return ... (5 Replies)
Discussion started by: Cheranime
5 Replies

10. Shell Programming and Scripting

Problem while taking input from User

Hi All, I am facing a problem while taking input from user in Shell Script. Basically , I want to read a file line by line. And I want to remove certain lines of that file.But before removing those lines , I need to ask the user whether user wants to remove the lines. If user inputs "Yes" , the... (3 Replies)
Discussion started by: swapnil.nawale
3 Replies
Login or Register to Ask a Question