Linux - General This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-23-2011, 10:25 AM
|
#1
|
|
LQ Newbie
Registered: Oct 2011
Posts: 6
Rep: 
|
Create a math script using the bash shell
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.
|
|
|
|
11-23-2011, 11:10 AM
|
#2
|
|
Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 11,801
|
Quote:
Originally Posted by bigron1953
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.
|
We will be glad to HELP you, but we are not going to write your scripts for you. Post what you've written so far, and post where you're stuck. Otherwise, try Google, where you can find lots of scripting tutorials, examples, and other help.
http://tldp.org/LDP/abs/html/
http://www.codecoffee.com/tipsforlin...cles2/044.html
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.
|
|
|
|
11-26-2011, 03:51 AM
|
#3
|
|
Member
Registered: Apr 2004
Distribution: slack what ever
Posts: 707
Rep:
|
you can use bc to do the math in a script
man bc for the details
you may even be able to write the whole script in the bc language
|
|
|
|
11-27-2011, 08:44 AM
|
#4
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
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.
|
|
|
|
11-28-2011, 01:30 PM
|
#5
|
|
LQ Newbie
Registered: Oct 2011
Posts: 6
Original Poster
Rep: 
|
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
if [ $choice -eq 1 ]
then
answer="$n1 * $n2 * $n3 = $(( $n1 * $n2 * $n3 ))"
elif [ $choice -eq 2 ]
then
answer="$n1 + $n2 + $n3 = $(( $n1 + $n2 + $n3 ))"
elif [ $choice -eq 3 ]
then
answer="$n1 / $n2 / $n3 = $(( $n1 / $n2 / $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.
|
|
|
|
11-28-2011, 06:24 PM
|
#6
|
|
Senior Member
Registered: Apr 2008
Distribution: PCLinux, Ubuntu, Peppermint
Posts: 3,393
|
Replace answer with echo or printf, delete the = sign but leave a space.
|
|
|
1 members found this post helpful.
|
11-29-2011, 08:39 AM
|
#7
|
|
LQ Newbie
Registered: Oct 2011
Posts: 6
Original Poster
Rep: 
|
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
echo "1. Add"
echo "2. Multiplication"
echo "3. Average"
echo "4. Exit"
echo -n "Enter number 1 ---->"
read num_1
echo -n "Enter number 2 --->"
read num_2
echo -n "Enter number 3 -->"
read num_3
echo -n "Enter number 4 ->"
read num_4
}
case $number in
echo "number 1 + number 2 + number 3 = $(( num_1 + num_2 + num_i3 ))"
echo "number 1 * number 2 * number 3 = $(( num_1 * num_2 * num_3 ))"
echo "number 1 + number 2 + number 3 = $(( num_1 + num_2 + num_3/3 ))";;
*)Exit
;;
esac
# And this is the error message i got:
[ron@localhost ~]$ ./testfile3.sh
./testfile3.sh: line 35: syntax error near unexpected token `"number 1 + number 2 + number 3 = $(( num_1 + num_2 + num_i3 ))"'
./testfile3.sh: line 35: `echo "number 1 + number 2 + number 3 = $(( num_1 + num_2 + num_i3 ))"'
[ron@localhost ~]$
|
|
|
|
11-29-2011, 12:25 PM
|
#8
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
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:
http://mywiki.wooledge.org/BashGuide
http://www.linuxcommand.org/index.php
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://wiki.bash-hackers.org/start
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
|
|
|
1 members found this post helpful.
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:47 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|