我试图构建一个基本的计算器,但使用函数。我一次又一次地得到错误第17行:乘法:找不到命令。所以我想我把函数调用错了?那么,调用函数和传递参数的正确方法是什么?
#!/bin/bash
echo "Enter the operation:"
read operation
echo "Operand 1:"
read operand_1
echo "Operand 2:"
read operand_2
if [[ "$operation" == "+" ]]
then
addition
elif [[ "$operation" == "-" ]]
then
subtraction
elif [[ "$operation" == "*" ]]
then
multiplication
elif [[ "$operation" == "/" ]]
then
division
fi
addition()
{
result = $((operand_1+operand_2))
result $result
}
subtraction()
{
result = $((operand_1-operand_2))
result $result
}
multiplication()
{
result = $((operand_1*operand_2))
result $result
}
division()
{
result = $((operand_1/operand_2))
result $result
}
result()
{
echo "The result is $1"
}
在bash脚本中调用函数之前,需要定义这些函数