#!/bin/bash
if [ $# -lt 3 ] ;then
echo "USAGE : calculate.sh VAR1 OPERATOR VAR2"
exit 1
fi
VAR1=$1
OP=$2
VAR2=$3
if [ $OP = '+' ];then
echo "$VAR1+$VAR2=$[$VAR1+$VAR2]"
exit 0
elif [ $OP = '-' ];then
echo "$VAR1-$VAR2=$[$VAR1-$VAR2]"
exit 0
elif [ $OP = '*' ];then
echo "$VAR1*$VAR2=$[$VAR1*$VAR2]"
exit 0;
else
echo "$VAR1/$VAR2=$[$VAR1/$VAR2]"
fi
The above is the content of calculate.sh
.
If I use +
, -
, or /
, I get the correct answer, but when I use *
, it reports an error:
kdyzm@kdyzm:~/scripts$ ./calculate.sh 2 + 3 2+3=5 kdyzm@kdyzm:~/scripts$ ./calculate.sh 2 - 3 2-3=-1 kdyzm@kdyzm:~/scripts$ ./calculate.sh 2 * 3 ./calculate.sh: line 21: 2/command.sh: syntax error: invalid arithmetic operator (error token is ".sh") kdyzm@kdyzm:~/scripts$ ./calculate.sh 2 / 3 2/3=0 kdyzm@kdyzm:~/scripts$
How can I resolve this problem?