【问题标题】:How to call functions correctly and pass parameter through them using bash [duplicate]如何正确调用函数并使用 bash [重复] 通过它们传递参数
【发布时间】: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"
}

【问题讨论】:

标签: bash function shell parameters sh


【解决方案1】:

您需要在 bash 脚本中调用它们之前定义函数

【讨论】:

    猜你喜欢
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2019-02-23
    相关资源
    最近更新 更多