Sponsored Content
Top Forums Shell Programming and Scripting Not able to exit from case statements Post 302366124 by uxpassion on Thursday 29th of October 2009 12:40:41 AM
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 02:20 AM..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
ARITHMETIC(6)							 BSD Games Manual						     ARITHMETIC(6)

NAME
arithmetic -- quiz on simple arithmetic SYNOPSIS
arithmetic [-o +-x/] [-r range] DESCRIPTION
arithmetic asks you to solve problems in simple arithmetic. Each question must be answered correctly before going on to the next. After every 20 problems, it prints the score so far and the time taken. You can quit at any time by typing the interrupt or end-of-file character. The options are as follows: -o By default, arithmetic asks questions on addition of numbers from 0 to 10, and corresponding subtraction. By supplying one or more of the characters +-x/, you can ask for problems in addition, subtraction, multiplication, and division, respectively. If you give one of these characters more than once, that kind of problem will be asked correspondingly more often. -r If a range is supplied, arithmetic selects the numbers in its problems in the following way. For addition and multiplication, the numbers to be added or multiplied are between 0 and range, inclusive. For subtraction and division, both the required result and the number to divide by or subtract will be between 0 and range. (Of course, arithmetic will not ask you to divide by 0.) The default is 10. When you get a problem wrong, arithmetic will remember the numbers involved, and will tend to select those numbers more often than others, in problems of the same sort. Eventually it will forgive and forget. arithmetic cannot be persuaded to tell you the right answer. You must work it out for yourself. DIAGNOSTICS
``What?'' if you get a question wrong. ``Right!'' if you get it right. ``Please type a number.'' if arithmetic doesn't understand what you typed. SEE ALSO
bc(1), dc(1) BSD
May 31, 1993 BSD
All times are GMT -4. The time now is 06:56 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy