如何使用bash正确调用函数并通过函数传递参数



我试图构建一个基本的计算器,但使用函数。我一次又一次地得到错误第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脚本中调用函数之前,需要定义这些函数

相关内容

  • 没有找到相关文章

最新更新