Today I have started to learn to write bash shell scripts, and the prof has tasked us to create a simple script calculator using three inputs: two of which are numeric inputs and one is an operator input.
First, to my understanding, when a variable is assigned from reading input, it is treated as a string. so then by comparing the variable op
to the string version of +
it should evaluate n1+n2
. however this is not the case, and I get the following errors:
./q2.txt: line 9: [+: command not found
./q2.txt: line 13: [+: command not found
#!/bin/bash
echo "Enter the operator"
read op
echo "Enter the first number"
read n1
echo "Enter second number"
read n2
if ["$op" == "+"]
then
n3=$((n1+n2))
elif [$op = "-"]
then
n3 = $((n1-n2))
fi
echo "Answer: $n3"
exit 0