Bash – “integer expression expected” error

bashshell-script

I am running the following script:

#!/bin/bash
# This script acts as a simple calculator for add, subtract, multiply and divide.
echo "Kindly ENTER 'a' to select for addition"
echo "Kindly ENTER 's' to select for subtraction"
echo "Kindly ENTER 'm' to select for multiplication"
echo "Kindly ENTER 'd' to select for division"
read oper
echo "Please ENTER any number of your choice"
read no1
echo "Please ENTER another number of your choice"
read no2
if [ $oper -eq a ]; then 
echo "Your addition result is: $(($no1 + $no2))" 
elif [ $oper -eq s ]; then 
echo "Your subtraction result is: $(($no1 - $no2))"
elif [ $oper -eq m ]; then 
echo "Your multiplication result is: $(($no1 * $no2))"
elif [ $oper -eq d ]; then 
echo "Your division result is: $(($no1 / $no2))"
else echo "Your selection from the begining was incorrect"
fi

This is the error/output:

./test2.sh: line 12: [: m: integer expression expected
./test2.sh: line 13: [: m: integer expression expected
./test2.sh: line 14: [: m: integer expression expected
./test2.sh: line 15: [: m: integer expression expected
Your selection from the begining was incorrect

What could be the cause please?

Best Answer

The -eq operator is a relational operators that are specific to integer values. These operators do not work for string values unless their value is integer.

So use = which checks if the value of two string operands are equal or not

Related Question