A
A
Artem Melnykov2019-06-18 15:54:25
linux
Artem Melnykov, 2019-06-18 15:54:25

How to solve if..else problem in bash?

Hello!
I'm writing a game:

#! /bin/bash
while true
do
        a=$((1 + RANDOM % 10))
        b=$((1 + RANDOM % 10))
        res=$a+$b
        echo -n "$a + $b = "
        read path
        echo ""
        if 
        then
                echo "It's true"
        else
                echo "It's false!!!"
        fi
done

However, it always outputs
It's false!!!

What to do???

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Saboteur, 2019-06-18
@NickProgramm

By default, bash creates all variables as text, so
res=$a+$b gives you string concatenation, not number addition.
Use explicit arithmetic addition through res=$(( $a + $b ))
or declare all variables in advance as integer through
declare -ia
declare -ib
declare -i res
declare -i path
And ideally, comparison instead of make it numeric

L
Lynn "Coffee Man", 2019-06-18
@Lynn

And you look what you have $res
in
res=$(( $a + $b ))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question