Using menu driven script

Hi Team ,
I wrote a shell script for adding and subtracting two numbers am getting error could some one please help to fix it
script:

echo "Enter  1 to add:"

echo "Enter 2 to  sub:"


echo "Enter  3  for  both addition and subtraction :"
read ans;

 case "$ans" in

   1) echo "To  add  two numbers"
      echo "Enter the  value of  a :" ;;

     read a

      echo "Enter the value of b:"
     read b

     adding_numbers

   2) echo "To  sub  two numbers"
      echo "Enter the  value of  a :" ;;

      read a

      echo "Enter the value of b:"
       read b

      sub_numbers

  3) echo "To perform both addition and Subtraction"

     echo "Enter the  value of  a :" ;;

     read a

     echo "Enter the value of b:"
     read b

     adding_numbers &  sub_numbers

    *) echo "$ans is an invalid option. Choose between 1-2 only" ;

error am getting is:

1
combo.sh: line 33: syntax error near unexpected token `a'
combo.sh: line 33: `     read a'

Is this a homework assignment?

Homework and coursework questions can only be posted in the Homework & Coursework Forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

If this is not a homework question, please explain what you are working on, how this script will be used, what operating system you're using, and what shell you're using. Without this basic information, we can waste a lot of time making assumptions about what might or might not work in your environment.

Thank You.

The UNIX and Linux Forums.

Hi Don ,

am a working IT professional by using this format i can prepare for my professional work, since i cannot reveal my office code in the public forum,

Regards
Kanna

Hi,

If you are really, truly needing to provide some way for your end users in a production environment to add or subtract numbers at the command line, there's no sense in re-inventing the wheel here. UNIX already provides a fair number of ways to do this, the easiest of which is the bc utility.

It can be run interactively, like so:

$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2+2
4
4-2
2
$

Or you can pipe things into it:

$ echo 2+2 | bc
4
$ echo 4-2 | bc
2
$

Or it can read its input from a file:

$ cat calc.txt
2+2
4-2
$ bc < calc.txt
4
2
$

and so on and so forth. It really would be a complete waste of your own time to write a shell script specifically to do calculation, when UNIX provides a built-in utility for doing precisely this.

Now, if this is a homework assignment (and personally, I really can't see any reason why you'd need to write a script like this outside of academia) that's absolutely fine - honestly, we really don't mind, and are always happy to help. But you would need to follow the homework rules as mentioned by Don Cragun, and as outlined here.

Alternatively, if there's some truly bizarre and currently-unimaginable reason why you absolutely MUST write such a script in a production environment, please let us know exactly what that is and I'm sure we can help. But honestly, bc is such a better answer than anything any one person could ever write a shell script to replace. Your users' own desktop OS calculator applet would be a better answer, to be frank.

Hope this helps.

1 Like

Since you refuse to tell us for whom you work, what shell you're using, and what operating system you're using; I have to say that this looks like homework and not like a serious piece of code that any IT professional would release to a normal user.

Therefore, I can only say that my guess at your problem is that your code does not follow the syntax used by a shell that accepts Bourne shell syntax for a case statement. Bourne shell case statement syntax does not allow any code like read statements between the ;; ending the code to be executed for a given pattern and the start of the next pattern matching expression. Your code is also missing the line of code that terminates every case statement. And, of course, your code is using two functions but never defines those functions. It is also unclear to me why you would want to run those two functions concurrently instead of running them sequentially in the code that both adds and subtracts numbers.

You say that your code is supposed to add and subtract numbers, but you haven't said what is supposed to be done with the calculated sum and difference. You haven't specified the format of the numbers to be processed (unsigned or signed numbers; integer, floating point, or complex numbers; decimal only or decimal, octal, hexadecimal, and/or other bases; etc.).

Therefore, there is no way for us to guess at what code you need to reach your goal. And, until you give us some reason to believe that this is not homework (like several other requests we have seen in the past asking for help with VERY similar tasks that were homework assignments) we can't take the chance of doing your homework for you in violation of several educational institutions' guidelines of institutions that support the work we do at The UNIX & Linux Forum.

1 Like