LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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



Reply
 
Search this Thread
Old 11-23-2011, 11:25 AM   #1
bigron1953
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Rep: Reputation: Disabled
Smile 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 01:01 AM. Reason: To add script.
 
Old 11-23-2011, 12:10 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,103

Rep: Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719
Quote:
Originally Posted by bigron1953 View Post
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.
 
Old 11-26-2011, 04:51 AM   #3
rob.rice
Member
 
Registered: Apr 2004
Distribution: slack what ever
Posts: 797

Rep: Reputation: 119Reputation: 119
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
 
Old 11-27-2011, 09:44 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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.
 
Old 11-28-2011, 02:30 PM   #5
bigron1953
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-28-2011, 07:24 PM   #6
yancek
Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 5,118

Rep: Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817Reputation: 817
Replace answer with echo or printf, delete the = sign but leave a space.
 
1 members found this post helpful.
Old 11-29-2011, 09:39 AM   #7
bigron1953
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
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 ~]$
 
Old 11-29-2011, 01:25 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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 03:21 PM. Reason: clean-up
 
1 members found this post helpful.
  


Reply

Tags
bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: The Bash Shell: Doing Your Math LXer Syndicated Linux News 0 01-28-2011 02:50 PM
Loops in Bash shell or variable math... edpatterson Programming 4 10-29-2010 02:09 AM
How to do simple math in shell script gidrow Programming 5 07-30-2009 04:46 PM
script Q : how do I force bash to perform the math? kevinyeandel Linux - Newbie 4 02-20-2009 03:35 AM
Using vi to create bash shell script BBFeltham Linux - Newbie 8 07-28-2008 09:02 PM


All times are GMT -5. The time now is 05:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration