如何在bash中调用带参数的函数?

时间:2016-04-07 14:21:27

标签: bash

我有一个用于测试软件的功能。我必须调用此函数,其中函数有2个参数,即currentSetName和failureDetected。这是代码:

#!/bin/bash

failureDetected=0 
nbOfAllTests=0
nbOfDetectedFailures=0`
currentSetName="fair"
count_input=1

removeFromCurrentSet(){
if [[("$1" == "good")]]
  then 
      #mv Partition/currentSet/* Partition/good_used
      echo "Hai good"

  elif [[("$1" == "fair")]]
  then
        if [[("$2" == "0")]]
        then
            #mv Partition/currentSet/* Partition/poor_used
            echo "hai fair poor_used"
        else 
            #mv Partition/currentSet/* Partition/good_used
            echo "hai fair good"
        fi
else

            #mv Partition/currentSet/* Partition/poor_used
            echo "jjj poor"

fi
}

removeFromCurrentSet currentSetName failureDetected

我使用上面的代码,但效果不佳。你能帮我解决这个问题吗?我怎样才能完全调用该函数?

1 个答案:

答案 0 :(得分:3)

您错误地传递了参数。您的原始代码等同于

removeFromCurrentSet "currentSetName" "failureDetected"

只传递两个字符串,因为currentSetNamefailureDetected未展开。您需要先使用$扩展变量。

要传递给函数,变量的值会尝试这样:

removeFromCurrentSet "${currentSetName}" "${failureDetected}"