LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-26-2011, 08:33 AM   #1
ugurgazi
LQ Newbie
 
Registered: Apr 2011
Posts: 10

Rep: Reputation: 0
Doing math in bash?


Hello!

I saw that there were a few differente ways to do math within bash scripts.
expr and bc are possibilities. But whichone to use when?

I want to make an simple bash script for children to do math.
The script must ask only questions like 1*1= till 10*10= ...
No + or - or / only * questions.

When the good answer is given to a questions, the next question must be shown.
When the wrong answer is given to a question, the same question must be repeated till the good answer is given.
The output must be logged to a file. the questions begins from 1*1 till 1*10, and the same for 2*1 till 2*10, 3, 4, 5, 6, 7, 8, 9 etc.....

How can I accomplish this, or can you help me out with it?
 
Old 04-26-2011, 08:48 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,833
Blog Entries: 1

Rep: Reputation: 1230Reputation: 1230Reputation: 1230Reputation: 1230Reputation: 1230Reputation: 1230Reputation: 1230Reputation: 1230Reputation: 1230
I'd recommend you start writing your script and ask specific questions when you are stuck at some point. It's rather unlikely that anyone here is going to write the script for you.

Check the following tutorial. You should find all the things you need there.
http://linuxconfig.org/Bash_scripting_Tutorial

Have fun
 
Old 04-26-2011, 09:44 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
As it is integer multiplication you are looking at I would simply use (()) [assuming it has to be bash], but as sycamorex has said, until you show where you are stuck it is hard to help you further.
 
1 members found this post helpful.
Old 04-26-2011, 01:07 PM   #4
ugurgazi
LQ Newbie
 
Registered: Apr 2011
Posts: 10

Original Poster
Rep: Reputation: 0
you are right. I have the next script:


#!/bin/bash
i=1
while [ $i -le 10 ]; do
j=1
while [ $j -le 10 ]; do
echo "$i * $j = $((i*j))"
((j+=1))
done
((i+=1))
done



How can we now change this, so that one question will be asked one by one.

It's like 3*3 = .... the question must be filled in. If the correct answer is given than the next question will be asked, either the same question will be repeated till the correct answer is given.

Will the command trap be used in here? Or is something more need to be done..
 
Old 04-26-2011, 01:18 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Could you indent the code and put it in [code] tags?
 
Old 04-26-2011, 02:12 PM   #6
ugurgazi
LQ Newbie
 
Registered: Apr 2011
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:

#!/bin/bash
i=1
while [ $i -le 10 ]; do
j=1
while [ $j -le 10 ]; do

echo "$i * $j = $((i*j))"
((j+=1))
done
((i+=1))
done
is this ok?
 
Old 04-26-2011, 02:18 PM   #7
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 695

Rep: Reputation: 110Reputation: 110
Code:
#!/bin/bash
i=1
while [ $i -le 10 ]
do
   j=1
   while [ $j -le 10 ]
   do
       answer=0
       while [ $answer != (($i*$j)) ]
       do
            echo -n "$i * $j = "
            read answer
       done
       ((j+=1))
   done
   ((i+=1))
done
I think you're looking for something like this if you want to ask; (I wasn't 100% of the parameters to "read", but the echo can be put into the read command.)
 
Old 04-26-2011, 02:49 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by ugurgazi View Post
is this ok?
[code] tags, not [quote] tags!
 
Old 04-26-2011, 07:49 PM   #9
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 46
I recommend you use bc/awk for maths that involve floats/decimal points. Or use a language other than bash for what you are doing.
 
Old 04-26-2011, 08:07 PM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by kurumi View Post
I recommend you use bc/awk for maths that involve floats/decimal points. Or use a language other than bash for what you are doing.
But the math that the OP is doing is integer only.
 
Old 04-26-2011, 09:06 PM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
I think Ramurd is on the right idea although why use 2 while loops when you have effectively created 2 for loops?
Code:
#!/bin/bash

for (( i = 1; i <= 10; i++ ))
do
   for (( j = 1; j <= 10; j++ ))
   do
       answer=0
       while (( answer != i * j ))
       do
            read -p "$i * $j = " answer

            [[ ${answer,,} == q ]] && exit
       done
   done
done
 
Old 04-27-2011, 01:13 AM   #12
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 46
Quote:
Originally Posted by MTK358 View Post
But the math that the OP is doing is integer only.
he is doing a program for children to do maths. If that's the case, simple floating point maths like 1.5 * 2 could be a possible question to ask. In the mathematical world, integers are not the only numbers that exists. Even ksh supports floating point from long ago. Its a mystery why bash doesn't support it even till now. Further, OP might eventually want to port his program to be a GUI one. In that case, bash or any shell scripting is not going to cut it. He might as well start using a programming language now with GUI support than struggle to rewrite them in future.
 
Old 04-27-2011, 01:43 AM   #13
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 695

Rep: Reputation: 110Reputation: 110
Quote:
Originally Posted by kurumi View Post
he is doing a program for children to do maths. If that's the case, simple floating point maths like 1.5 * 2 could be a possible question to ask. In the mathematical world, integers are not the only numbers that exists. Even ksh supports floating point from long ago. Its a mystery why bash doesn't support it even till now. Further, OP might eventually want to port his program to be a GUI one. In that case, bash or any shell scripting is not going to cut it. He might as well start using a programming language now with GUI support than struggle to rewrite them in future.
Rather than thinking up what someone might want to do in the future, the question was how to do simple math in shell-script (if such is simple enough); As you can see the script was simple, easy and short enough that this is indeed a good way to start out; If you want to do more special stuff, you can still do your maths with bc:
echo "$i * $j" | bc -l
but at this point bc is simply overkill;

If putting the same functionality in a c-program, it'd still be simple and easy, true enough: but that was not the question, nor what he might want to do in the future, nor that it was to become a GUI application, nor ... While what you state is in fact true, it's not the question; This is one of the reasons projects tend to go out of control: IT people have a tendency not to stick to the assignment/question, but rather agree to do more than is initially agreed upon etc.
 
Old 04-27-2011, 07:31 AM   #14
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by kurumi View Post
he is doing a program for children to do maths...
Or maybe he's practicing BASH scripting.
 
Old 04-28-2011, 02:56 AM   #15
ugurgazi
LQ Newbie
 
Registered: Apr 2011
Posts: 10

Original Poster
Rep: Reputation: 0
Yes, practicing, and making a script for the children.
So far thanks for your information. Together with the above script I have a second script. (see below)

What goes wrong is, when a question is asked wrong, it will repeat this question. So far so good.
But when the second time the answer to the question is given correct, it still repeats!
I think there is something wrong with the "else.......read $getallinks x $getalrechts" part... But what is?

It has to repeat the question with the wrong answer till the correct answer is given.. !

Still learning much with bash scripting.. Thanks to you!


******** THE SCRIPT ********
Quote:
#!/bin/bash

while true
do
getallinks=$((RANDOM/(32767/10)+1))
getalrechts=$((RANDOM/(32767/10)+1))

echo "Hoeveel is $getallinks x $getalrechts ?"
read -p "Antwoord: " antwoordleerling
antwoordjuiste=$((getallinks*getalrechts))

if test $antwoordleerling == $antwoordjuiste
then
echo "Goed zo! $getallinks x $getalrechts is inderdaad $antwoordjuiste."
else

while (( $antwoordleerling != $antwoordjuiste ))
do
echo "Fout! Probeer het nogmaals $getallinks x $getalrechts ="
read $getallinks x $getalrechts

done
fi
done
 
  


Reply


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 Off
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 01:50 PM
Loops in Bash shell or variable math... edpatterson Programming 4 10-29-2010 01:09 AM
[SOLVED] Issues doing simple math in Bash script buee Linux - Newbie 10 07-20-2010 10:46 PM
bash math...can't divide!! vous Programming 9 07-07-2009 03:22 AM
bash and math division problem bennethos Programming 5 10-17-2004 01:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:38 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration