# Thread: 1 + 1 , 2 + 2. 3 + 3, 4 + 4 answers in bash script

Enjoy an ad free experience by logging in. Not a member yet? Register.
1. ## 1 + 1 , 2 + 2. 3 + 3, 4 + 4 answers in bash script

t. For simplicities sake start with equation 1 + 1 and loop statements ten times, incrementing the 1 + 1 by 1 each time. So the second time through the statement would read 2 + 2 = and so forth. You need to keep track for the number of correct and incorrect responses. At the end give the user their score, remember the score is the number correct / 10.

#!/bin/bash

add=\$((\$a+\$a)) || ((\$b+\$b)) || ((\$c+\$c)) || ((\$d+\$d)) || ((\$e+\$e)) || ((\$f+\$f)) || ((\$g+\$g)) || ((\$h+\$h)) || ((\$i+\$i)) || ((\$j+\$j))

a=1 # initialize a to 1
b=2 # initialize b to 2
c=3 # initialize c to 3
d=4 # initialize d to 4
e=5 # initialize e to 5
f=6 # initialize f to 6
g=7 # initialize g to 7
h=8 # initialize h to 8
i=9 # initialize i to 9
j=10 # initialize j to 10
if [ "\$a+\$a" -eq 2 ] || [ "\$b+\$b" -eq 4 ] || [ "\$c+\$c" -eq 6 ] || [ "\$d+\$d" -eq 8 ] || [ "\$e+\$e" -eq 10 ] || [ "\$f+\$f" -eq 12 ] || [ "\$g+\$g" -eq 14 ] || [ "\$h+\$h" -eq 16 ] || [ "\$i+\$i" -eq 18 ] || [ "\$j+\$j" -eq 20 ]; then

echo "You are correct."
else
fi
fi
done

Can anyone show me why this script is wrong. I am new to Bash scripting.

2. It's against the forum rules to solve peoples homework problems, but we can help you get the correct answer.

First thing I see is the double "fi" statement at the end. "else" is a component of "if", therefore it only needs one "fi" statement.

Second, nowhere do you ask for user input.
Here is an easy way to do this

Code:
Here, -e enables the \c command to be interpreted in the echo command, and read answer will assign the user input to the variable answer

Also, I suggest using a loop for the equation instead of having all those different variables.
Something like this (there's some errors in here that need to be fixed).

Code:
#Make \$a = 1
a =1;

# Put the whole thing inside this while loop, this will increment \$a every time the user puts in an aswer.

# It will stop running once a = 11
while \$a <= 10 then

# Ask the question and get the user input

#increment a
\$a = \$a + 1;

done;
You'll need to check the answer as well.
Something like this
Code:
if \$a + \$a == \$answer; then;
echo "correct";
else;
echo "wrong";
fi;
Somewhere in there you will also need to add a counter for correct and incorrect answers.

3. I'm going to go one step further than Cancerous and close this thread. If you have a specific question about some difficulty, we're happy to help you figure it out, but we won't do your homework for you.

4. \$spacer_open \$spacer_close

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•