Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hi all can someone please help me i am trying to write a bash script that ask the user for three numbers and gives the user three options to choose from: add, multiply and average. would like to use a case statement to make this script work. save this as one function, give users another option to do something like this: ./math A 1 2 3 thescript will automatically add the numbers ./math M 2 3 4 will automatically multiply all the numbers etc.
And if the user simply enters ./math, then he'll present the case function.
Thanks
i hope somone can give me a step by step on this.
This is the script i wrote and it does work as is, but i'm not sure about how to use it in a case statement to make it work that way, and save it as one function.
#!/bin/bash
first_num=0
second_num=0
third_num=0
# get three numbers from user
echo -n "Enter the first number --->"
read first_num
echo -n "Enter the second number -->"
read second_num
echo -n "Enter the third number ->"
read third_num
echo "first number + second number + third number = $((first_num + second_num + third_num))"
echo "first number * second number * third number = $((first_num * second_num * third_num))"
echo "first number / second number / third number = $((first_num / second_num /third_num))"
I don't want anyone to write the script for me, its just that i can't find the information i seek on the internet.
Last edited by bigron1953; 11-27-2011 at 12:01 AM.
Reason: To add script.
Hi all can someone please help me i am trying to write a bash script that ask the user for three numbers and gives the user three options to choose from: add, multiply and average. would like to use a case statement to make this script work. save this as one function, give users another option to do something like this: ./math A 1 2 3 thescript will automatically add the numbers ./math M 2 3 4 will automatically multiply all the numbers etc.
And if the user simply enters ./math, then he'll present the case function.
Thanks
i hope somone can give me a step by step on this.
If you want a 'step-by-step', you've already outlined what you NEED above. Follow it..those are the steps. Now all you've got to do is research HOW you do them, which is probably the purpose of your homework assignment.
Be aware that bash, like most shells, is only capable of simple integer math. For floating point math you need to use an external tool like bc or awk.
You also shouldn't post assuming that any specific form or tool will be suitable for your situation. It may be that case is completely the wrong thing to use. (But actually you probably will want to use one somewhere in this script. )
What you will want to do is start by parsing the $1 parameter for the operating letter. You can indeed use a case statement to evaluate it if you want, and use it to launch different commands based on the letter found, perhaps a function that does different things based on how you call it. You may want to have a look at the shift built-in at this point.
And please use [code][/code] tags around your code, to preserve formatting and to improve readability.
Hi my first post was that i am trying to write a bash script that ask the user for three numbers and gives the user three options to choose from: add, multiply and average. would like to use a case statement to make this script work. save this as one function,and so on. I have a new script with a menu that i wrote and it does ask the user for three numbers and gives the user three options:add,mul,div. but it is not working right because i get an error message. hear is my script:
#!/bin/bash
# Script to display sum of three numbers and to do calculations such as *, + / etc
#
echo "**** My calculator ****"
echo "M A I N - M E N U"
echo "1. Multiplication"
echo "2. Addition"
echo "3. Division"
echo -n "Please select your choice (1-3) : "
read choice
echo -n "Enter your first number : "
read n1
echo -n "Enter your second number : "
read n2
echo -n "Enter your third number : "
read n3
else
echo "Sorry please select number between 1-3 only"
exit 1
fi
And this is what happens when i run the script:
[ron@localhost ~]$ ./testscript2.sh
**** My calculator ****
M A I N - M E N U
1. Multiplication
2. Addition
3. Division
Please select your choice (1-3) : 1
Enter your first number : 5
Enter your second number : 4
./testscript2.sh: line 30: unexpected EOF while looking for matching `"'
./testscript2.sh: line 35: syntax error: unexpected end of file
[ron@localhost ~]$
Can someone please tell me what i am doing wrong thanks much.
I am now trying to use functions and case togather but it,s not work out can someone take a look at my code and error and tell me what you think.
Many thanks.
# What i want this script to do is:
#1. Construct a case statement with a user input variable.
#2. Have script capture the user input and process it with the case statement.
#3. The script needs to ask user for four inputs,ask user for direction on what to do with the
# inputs (eather Add,Multiplication,Average and Exit).
#4. If script detects Add, then add the numbers, if Multiplication then multiply and so on.
# This is the code i used:
#!/bin/bash
#using a Function
#Function number() just prints four numbers.
number()
{
echo "Enter your number"
sleep 2
number 1 2 3 or 4 # choice of numbers
First of all, I asked you to use code tags, in order to preserve formatting. Of course, that also means that you should have formatting to preserve. Proper use of indentation and spacing helps to make code more readable and debuggable.
Regarding the above script, you really need to study how shell constructions like functions and case statements actually work.
Here's a simple example of a function:
Code:
greetings(){
echo "This is the 1st greeting: $1"
echo "This is the 2nd greeting: $2"
echo "This is the 3rd greeting: $3"
}
greetings Hi Hello Goodbye
A function acts like a script within a script, and the function name is a command, not a variable. You call the function just like you would call ls, grep, echo, etc.
Note that the $1, $2, $3 values here are the function's input arguments, not those of the main script.
Here's the same function with a case statement inside it:
Code:
greetings(){
case $1 in
Hi) echo "Did you say Hi? Well Hello to you too!" ;;
Hello) echo "Did you say Hello? Well Hi to you too!" ;;
Goodbye) echo "You say Goodbye, I say Hello." ;;
*) echo "I don't understand you." ;;
esac
}
greetings Hi
greetings Goodbye
greetings Sayonara
greetings Hello
A case statement is a construct that tests text strings for the existence of patterns. You supply the input string on the first line, usually through a variable or other substitution, and it tries to match it to the globbing patterns at the beginning of the lines following it. The first pattern that matches, if any, will have the commands following it executed.
Now does the script you posted above use either of these forms properly?
Before you continue on with this script, I suggest you take time out to familiarize yourself more thoroughly with the fundamental concepts of scripting. Here are a few useful bash scripting references. I highly recommend the first link in particular, which is a good summary of the basic things you need to know:
Then, when you're ready to continue, you can try out what I suggested before. What you probably want to do is use a case statement to evaluate the first input argument to the script, and use that to call one or more functions that do the actual calculation work, passing the input arguments to the function.
And finally, again as I mentioned before, take a look at the shift command. Once the first argument is evaluated, or at least safely stored elsewhere, you can use it to remove it from the input list.
Last edited by David the H.; 11-29-2011 at 02:21 PM.
Reason: clean-up
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.