Using menu driven script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using menu driven script
# 1  
Old 02-01-2018
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:
Code:
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:

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


Last edited by Don Cragun; 02-01-2018 at 01:16 AM.. Reason: Add CODE tags.
# 2  
Old 02-01-2018
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.
# 3  
Old 02-01-2018
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
# 4  
Old 02-01-2018
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:

Code:
$ 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:

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

Or it can read its input from a file:

Code:
$ 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.
This User Gave Thanks to drysdalk For This Post:
# 5  
Old 02-01-2018
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.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Menu Driven Bash Shell Script with Default Option

Hi All, I have written a menu driven bash shell script. Current Output is as below: ------------------------------------- Main Menu ------------------------------------- Option 1 Option 2 Option 3 Option 4 Exit ===================================== Enter your... (3 Replies)
Discussion started by: kiran_j
3 Replies

2. Shell Programming and Scripting

Execution Problem with dispalying file content using menu driven script

HI All.. below is my menu options script. in option 2,3 and 4 im giving input and they are saving into their respective text file. problem is when im trying to "cat" those files in options 7,8 and 9 im not getting the output. no respective file contents are displaying on screen. but if i... (1 Reply)
Discussion started by: saichand1985
1 Replies

3. Shell Programming and Scripting

Use of stty vs trap in script-driven login menu

My employers would like me to selectively run one of several different (already-existing) Korn Shell menu-driven scripts out of the user's .profile file, depending on some yet-to-be-specified user critieria. I've never done this kind of thing, but I have the existing scripts (among other... (5 Replies)
Discussion started by: Clovis_Sangrail
5 Replies

4. Shell Programming and Scripting

Menu driven script.

I'm a beginner at scripting and have been putting this script together over the past week. It's no where as polish as it could be. Any tips/suggestions on improving this script would be appreciate it. Every week, my team develops WAR files in tomcat on our test environment and moves them to our... (4 Replies)
Discussion started by: bouncer
4 Replies

5. Shell Programming and Scripting

Help needed in writing a menu driven script

Hi, I was wondering if someone could help me write a shell script in Linux that backsup/restores data to anywhere I choose but it needs to be menu driven? Thanks, I'm new to Linux/Unix but liking it so far...just hoping to get to grips with the scripts! :) (7 Replies)
Discussion started by: Nicole
7 Replies

6. UNIX for Dummies Questions & Answers

What is a menu or command line option driven script?

i'm confused what this means. i was asked to design a menu or command line option driven script that reads out of a DB and displays info such as read_data.pl -u <user> -e <event> which would print commands run by <user>with the <event> in the db. any suggestions? i've been using... (2 Replies)
Discussion started by: kpddong
2 Replies

7. SuSE

Write shell script using menu-driven approach to show various system

QUESTION: Write shell script using menu-driven approach to show various system configuration like 1) Currently logged user and his logname 2) Your current shell 3) Your home directory 4) Your current path setting 5) Your current working directory 6) Show Currently logged number of... (1 Reply)
Discussion started by: bboyjervis
1 Replies

8. Shell Programming and Scripting

Menu driven Script needed ..pls help

Hi Guys.. am new to unix scrpiting..I need a Menu need to create using shell scrpting eg: Food items ready paid if i press "f" need to add items for a file food items.. if i press "r" it need to move into ready and remove from food items if i press "p" need to update a filed in... (1 Reply)
Discussion started by: sasdua
1 Replies

9. Shell Programming and Scripting

Assigning values to reference variables for a dynamic menu driven script.

How do I assign values to reference variables? I am assigning a variable name to --> $user_var Then I am trying to change its underlying variable value by $((user_var))=$user_value .. its failing,, Please let me know if there is a way to do this dynamically.. FileA.props... (5 Replies)
Discussion started by: kchinnam
5 Replies

10. Homework & Coursework Questions

Menu Driven Shell Script which accepts1 to 5 options

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: 1) Write a Menu Driven Shell Script which accepts1 to 5 options and performs the following actions for... (1 Reply)
Discussion started by: vaghya
1 Replies
Login or Register to Ask a Question