Not able to exit from case statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Not able to exit from case statements
# 1  
Old 10-29-2009
Not able to exit from case statements

Hi all,

I wrote the following simple shell script to perform addition, subtraction, multiplication and division. In the below program, i am not able to exit from the script

Shell Script
-----------

Code:
#!/bin/sh
bgcal() {
cal=""
echo "Enter the Option Number: \c"
read cal
if [ $cal = 1 ]
then
echo "You have selected addition"
elif [ $cal = 2 ]
then
echo "You have selected subtraction"
elif [ $cal = 3 ]
then
echo "You have selected multiplication"
elif [ $cal = 4 ]
then
echo "You have selected division"
else
echo "selection invalid. Please choose between 1-4"
fi
case $cal
in
1) echo "Enter two numbers for addition"
   read a b
   c=`expr $a + $b`
   echo " Addition of $a + $b is $c"
   echo " Do you want to continue"
   read ans
   if [ ans -eq 'yes' ]
   then
   bgcal
   else
   exit 0
   fi;;
2) echo "Enter two numbers for subtraction"
   read a b
   c=`expr $a - $b`
   echo " Subtraction of $a - $b is $c"
   echo " Do you want to continue"
   read ans
   if [ ans -eq 'yes' ]
   then
   bgcal
   else
   exit 0
   fi;;
3) echo "Enter two numbers for multiplication"
   read a b
   c=`expr $a \* $b`
   echo "Multiplication of $a * $b is $c"
   echo " Do you want to continue"
   read ans
   if [ ans -eq 'yes' ]
   then
   bgcal
   else
   exit 0 
   fi;;
4) echo "Enter two numbers for Division"
   read a b
   c=`expr $a / $b`
   echo "Division of $a / $b is $c"
   echo " Do you want to continue"
   read ans
   if [ ans -eq 'yes' ]
   then
   bgcal
   else
   exit 0
   fi;; 
*) echo "Do you want to continue?"
    read opt
     if [ opt -eq "yes" ]
     then
      bgcal
     else
     exit 0
     fi;;
esac
 }
echo "Choose the type of operation"
echo "============================"
echo "1-Addition"
echo "2-Subtraction"
echo "3-Multiplication"
echo "4-Division"
bgcal

Output:

> ./t2.sh
Choose the type of operation
============================
1-Addition
2-Subtraction
3-Multiplication
4-Division
Enter the Option Number: 1
You have selected addition
Enter two numbers for addition
4 5
Addition of 4 + 5 is 9
Do you want to continue
no
Enter the Option Number: 2
You have selected subtraction
Enter two numbers for subtraction
5 3
Subtraction of 5 - 3 is 2
Do you want to continue
no
Enter the Option Number:

Kindly let me know where i am wrong.

Thanks in advance

Last edited by vino; 10-29-2009 at 03:20 AM..
# 2  
Old 10-29-2009
Hi,
Change this
Code:
read ans
   if [ ans -eq 'yes' ]

with:
Code:
read ans
  if [ $ans = 'yes' ]

# 3  
Old 10-29-2009
Even

Code:
if [ opt -eq "yes" ]

should be

Code:
if [ $opt =  "yes" ]

Make sure that variables always should have "$" as prefix when used in any expressions.

Last edited by panyam; 10-29-2009 at 04:43 AM..
# 4  
Old 10-29-2009
Thank you ripat and panyam. It works perfectly fine
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching for pattern in variable using case statements

i would like to search a variable for a pattern, without having make any calls to external tools. i have a code like this: COUNTPRO2="gine is very bad vine is pretty good" case "${COUNTPRO2}" in *vine*) factor=${COUNTPRO2} echo $factor ;; esac If the variable contains... (7 Replies)
Discussion started by: SkySmart
7 Replies

2. Shell Programming and Scripting

Convert to case statements from if/elif

Hello, I wrote the case on code but it mistakes. I am not sure. If/elif code: #!/bin/ksh you=$LOGNAME hour=`date | awk '{print substr($4, 1, 2)}'` print "The time is: $(date)" if (( hour > 0 && $hour < 12 )) then print "Good morning, $you!" elif (( hour == 12 )) then (7 Replies)
Discussion started by: Masterpoker
7 Replies

3. Homework & Coursework Questions

Case statements and creating a file database

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The assignment is posted below: Maintain automobile records in a database Write a shell script to create,... (1 Reply)
Discussion started by: Boltftw
1 Replies

4. UNIX for Dummies Questions & Answers

Handling return & exit statements

I have 2 shell scripts the primary one would load the other one which will have functions defined in it. Script 1: . /apps/bin/Script 2 function if then continue... .... fi Script 2: function() (10 Replies)
Discussion started by: Ariean
10 Replies

5. Shell Programming and Scripting

Help with case and exit

Hi all, Am writing a script that uses the case statement and it is not working the way that I expect it to be. Script so far is as below. What am expecting to happen is if the user enter neither Y or YES or N or NO, it is supposed to exit out of the script running only on_exit, otherwise,... (3 Replies)
Discussion started by: newbie_01
3 Replies

6. Shell Programming and Scripting

exit from case - called in a function

Hello Experts, I am building a shell where I need to use case structure. The structure is in a function as in the sample code below: # Shell mySh #!/bin/sh doThis(){ var=$1 case "$var" in IT) echo "ok 1 $var" ;; ... (7 Replies)
Discussion started by: hkansal
7 Replies

7. UNIX for Dummies Questions & Answers

How to combine case statements

Hi, I need to change military time to regular time. I know to use case to indicate whether a.m. or p.m. as follows: case "$hour" in 0? | 1 ) echo a.m.;; 1 ) echo p.m.;; * ) echo p.m.;; esac My question is how do I add the hour and minute... (2 Replies)
Discussion started by: karp3158
2 Replies

8. Shell Programming and Scripting

Problem with my case statements

Hi there, Im having some problems with this function, I pass two arguments to the function $1 $2 (Arguments are month and date inputted by the user) for some reason the case always fails... however in the cases defined below where it shouldnt fail the result is: if it fails with input... (6 Replies)
Discussion started by: Darklight
6 Replies

9. Shell Programming and Scripting

case statements

i need to use a case statement to do something when the user enters nothing at the prompt. i know about the if statement and that isnt' what i'm interested in using for this. i want to use case. heres the scenerio. a program asks a user for an input. i want to use a case statement to... (1 Reply)
Discussion started by: Terrible
1 Replies

10. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies
Login or Register to Ask a Question

Featured Tech Videos