LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Create a math script using the bash shell (http://www.linuxquestions.org/questions/linux-general-1/create-a-math-script-using-the-bash-shell-915127/)

bigron1953 11-23-2011 10:25 AM

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.

TB0ne 11-23-2011 11:10 AM

Quote:

Originally Posted by bigron1953 (Post 4531826)
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.

rob.rice 11-26-2011 03:51 AM

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

David the H. 11-27-2011 08:44 AM

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.

bigron1953 11-28-2011 01:30 PM

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.

yancek 11-28-2011 06:24 PM

Replace answer with echo or printf, delete the = sign but leave a space.

bigron1953 11-29-2011 08:39 AM

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 ~]$

David the H. 11-29-2011 12:25 PM

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.


All times are GMT -5. The time now is 12:42 PM.