我的 bash 脚本有问题。我必须使用运算符 * 进行乘法运算。相反,脚本通过扩展和使用脚本本身的名称作为运算符来困扰我。我尝试使用单引号,但它不起作用:(这是代码
#!/bin/bash -x
# Bash script that calculates an arithmetic expression
# NO PRECEDENCE FOR OPERATORS
# Operators: + - *
if [ "$#" -lt "3" ]
then
echo "Usage: ./calcola.scr <num> <op> <num> ..."
exit 1
fi
result=0
op=+
j=0
for i in "$@"
do
if [ "$j" -eq "0" ]
then
# first try
#result=$(( $result $op $i ))
# second try
let "result$op=$i"
j=1
else
op=$i
j=0
fi
done
echo "Result is $result"
exit 0