Doing simple math for children.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Doing simple math for children.
# 1  
Old 04-26-2011
Doing simple math for children.

Hello!

I saw that there were a few differente ways to do math within bash scripts.

expr and bc are possibilities. But which one 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?


10* Many, many thanks in advance Smilie
# 2  
Old 04-26-2011
How much have you tried??
Did you create a script already?

regards,
Usha
# 3  
Old 04-26-2011
I have not done anything, I was experimenting on different scripts with:
- bc
- expr
- asking questions like:
Code:
trap
trap 'echo thanks for the math' EXIT
MATH=$((9))
echo '3*3='
while read -p 'answer: '  answer 1
do
  if [ "answer1" = "GETAL" ]
  then
    echo 'Right!'
    exit
  fi
  echo 'wrong!'
done


So I am still experimenting to see what happens. I am not so good in bash, that why I ask some help.

Last edited by Franklin52; 04-26-2011 at 10:57 AM.. Reason: Please use code tags and indentation
# 4  
Old 04-26-2011
This will give you something to start with. Modify as required:
Code:
#!/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

Regards,
Mark.
# 5  
Old 04-26-2011
Yes what I see is the whole list. Thanks! That is great.

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..
# 6  
Old 04-26-2011
The thing is, with what you're trying to do, you'll have to check each input (if you want it to repeat until they get it right at least). Which, unless there's some math library I don't know about out there to check answers, means you'll have to put in the correct answers (like a switch statement or something, or at least that's what comes to mind first for me).

I'm looking at it right now and I'll let you know if I come up with something.

edit:
I think this might be what you're looking for...
Code:
 
#!/bin/bash
i=1
while [ $i -le 10 ]; do
    j=1
    while [ $j -le 10 ]; do
      echo "$i * $j = "  
      read answer1
      if ["$answer1" -eq "$((i*j))"]
      then
        echo 'Right!'
        ((j+=1))
      else
        echo 'Wrong!' 
     fi     
    done
    ((i+=1))
done


Last edited by devrymike; 04-26-2011 at 05:45 PM.. Reason: code
This User Gave Thanks to devrymike For This Post:
# 7  
Old 04-27-2011
Almost!
But this looks very well devrmike.

What now happens is that I get the next while running the script:

Code:
~$   bash test1
1 * 1 = 
1
test1: line 8: [1: command not found
Wrong!

What happens in here? something wrong with
if ["$answer1" -eq "$((i*j))"] ??



I have also the next script which is similar.
Code:
#!/bin/bash
trap 'echo Very well, keep practicing' EXIT

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


Where in here do I put your answer is wrong or your answer is right as result, right before the next question is asked?

Last edited by pludi; 04-27-2011 at 05:25 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

pstree but without other children of ancestors

Hi, I want to display the process tree of a given PID, however, I don't want to see other children of the ancestors that don't reach the current PID. My goal is, from the tree result, i have to fetch a particular parent process by keyword, and if any other children from parents have the same... (2 Replies)
Discussion started by: ysrini
2 Replies

2. Programming

Multiple children and single pipe

Greetings everyone, I need a bit of help in solving the following problem: I'm given an array of numbers and I have to compute the sum of the array elements using n processes, and the inter process communication has to be done with pipes(one pipe, to be exact). I managed to solve the problem... (14 Replies)
Discussion started by: ephesos
14 Replies

3. Shell Programming and Scripting

Killing process and children

Hi all, I have been searching all day for a nice solution to this problem. I have three scripts. A start script, a child script and a stop script. Script A (scripta.sh) Its Child Script B (scriptb.sh) Script C (kill_process.sh $PID) Script A correctly traps the kill command sent from... (6 Replies)
Discussion started by: mark007
6 Replies

4. UNIX for Advanced & Expert Users

Fork() 1 Parent 3 Children

Hi, as I understand fork(), it makes a copy of the parent which becomes a child. But is there anyway to make three children for that one parent. So in other words, if I look up the getppid() of the children, I want them to have the same value?? Thanks in advance to any help! (1 Reply)
Discussion started by: MS_CC
1 Replies

5. Shell Programming and Scripting

Prepend name of directory to children folders

Hi, I am a shell scripting newbie. I am in need of a shell script that will prepend the name of the parent directory to the child directory. For example if the shell script called rename.sh is invoked with ">rename.sh /home/foobar/Simple" and the structure of the folder Simple is : Simple... (7 Replies)
Discussion started by: kalichar
7 Replies

6. UNIX for Dummies Questions & Answers

Simple Math question...

Hi All , I'm trying to do a simple math expression ...but unsuccessfull :-( Anyone can help me? days=23 amount=`expr ${days} / 30 \* -125` echo $amount but as result i got 0 when i expect 95.833333 Another question...how i can limit only to two or three decimal fields? Thanks in... (1 Reply)
Discussion started by: EDBGSK
1 Replies

7. UNIX for Advanced & Expert Users

How to follow processes and their children with ps

Hi, I often need to find the child processes of a parent process. There may be a string of 4-5. That is, PPID 884 spawns 890, which spawns 894, which spawns 1017. I'd like to be able to see all of them without having to type in a number of ps -ef commands. Process groups and session ID's are... (2 Replies)
Discussion started by: mschwage
2 Replies

8. Programming

Father & children

Hello, i should finished this program, if anyone could tell me whats wrong... This is an optional university work, though i cant leave this nearly finished. I need to see where is my error :( What my program should do. The user must type "num_proc" ( number of children). The program creates a... (4 Replies)
Discussion started by: Jariya
4 Replies
Login or Register to Ask a Question