【发布时间】:2021-09-20 19:11:35
【问题描述】:
我正在尝试构建一个基本的计算器,但使用的是函数。我一遍又一遍地收到错误line 17: multiplication: command not found。所以我想我把函数调用错了?那么调用函数和传递参数的正确方法是什么?
#!/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"
}
【问题讨论】:
-
不相关但接下来您可能会遇到的事情:赋值不允许在
=之前或之后使用空格,并且函数参数按数字引用:它必须是一个单词:result=$(($1 + $2))例如,用于添加。
标签: bash function shell parameters sh