从shell脚本中的被调用函数返回值

时间:2012-01-05 12:52:58

标签: shell return-value

我想从shell脚本中调用的函数返回值。也许我错过了语法。我尝试使用全局变量。但这也行不通。代码是:

lockdir="somedir"
test() {
    retval=""

    if mkdir "$lockdir"
        then    # Directory did not exist, but it was created successfully
            echo >&2 "successfully acquired lock: $lockdir"
            retval="true"
        else
            echo >&2 "cannot acquire lock, giving up on $lockdir"
            retval="false"
    fi
    return retval
}


retval=test()
if [ "$retval" == "true" ]
    then
        echo "directory not created"
    else
        echo "directory already created"
fi

5 个答案:

答案 0 :(得分:229)

Bash函数不能像您希望的那样直接返回字符串。你可以做三件事:

  1. 回音一个字符串
  2. 返回退出状态,这是一个数字,而不是字符串
  3. 分享变量
  4. 对于其他一些炮弹也是如此。

    以下是如何执行以下各项选项:

    1。回音字符串

    lockdir="somedir"
    testlock(){
        retval=""
        if mkdir "$lockdir"
        then # Directory did not exist, but it was created successfully
             echo >&2 "successfully acquired lock: $lockdir"
             retval="true"
        else
             echo >&2 "cannot acquire lock, giving up on $lockdir"
             retval="false"
        fi
        echo "$retval"
    }
    
    retval=$( testlock )
    if [ "$retval" == "true" ]
    then
         echo "directory not created"
    else
         echo "directory already created"
    fi
    

    2。返回退出状态

    lockdir="somedir"
    testlock(){
        if mkdir "$lockdir"
        then # Directory did not exist, but was created successfully
             echo >&2 "successfully acquired lock: $lockdir"
             retval=0
        else
             echo >&2 "cannot acquire lock, giving up on $lockdir"
             retval=1
        fi
        return "$retval"
    }
    
    testlock
    retval=$?
    if [ "$retval" == 0 ]
    then
         echo "directory not created"
    else
         echo "directory already created"
    fi
    

    3。分享变量

    lockdir="somedir"
    retval=-1
    testlock(){
        if mkdir "$lockdir"
        then # Directory did not exist, but it was created successfully
             echo >&2 "successfully acquired lock: $lockdir"
             retval=0
        else
             echo >&2 "cannot acquire lock, giving up on $lockdir"
             retval=1
        fi
    }
    
    testlock
    if [ "$retval" == 0 ]
    then
         echo "directory not created"
    else
         echo "directory already created"
    fi
    

答案 1 :(得分:14)

你的工作方式太难了。您的整个脚本应该是:

if mkdir $lockdir 2> /dev/null; then 
  echo lock acquired
else
  echo could not acquire lock >&2
fi

但即使这样也可能过于冗长。我会编码:

mkdir $lockdir || exit 1

但产生的错误信息有点模糊。

答案 2 :(得分:10)

如果它只是一个真/假测试,请将您的函数return 0用于成功,将return 1用于失败。然后测试将是:

if function_name; then
  do something
else
  error condition
fi

答案 3 :(得分:1)

我认为对于失败(glenn jackman)来说,为succ / 1返回0,而olibre的明确和解释性答案就是这样;只是提到一种"组合"对于结果不是二元的情况的方法,你更喜欢设置变量而不是"回显"一个结果(例如,如果你的函数也假设要回应一些东西,这种方法将不起作用)。然后怎样呢? (下面是Bourne Shell)

# Syntax _w (wrapReturn)
# arg1 : method to wrap
# arg2 : variable to set
_w(){
eval $1
read $2 <<EOF
$?
EOF
eval $2=\$$2
}

就像在(是的,这个例子有点傻,它只是一个例子)

getDay(){
  d=`date '+%d'`
  [ $d -gt 255 ] && echo "Oh no a return value is 0-255!" && BAIL=0 # this will of course never happen, it's just to clarify the nature of returns
  return $d
}

dayzToSalary(){
  daysLeft=0
  if [ $1 -lt 26 ]; then 
      daysLeft=`expr 25 - $1`
  else
     lastDayInMonth=`date -d "`date +%Y%m01` +1 month -1 day" +%d`
     rest=`expr $lastDayInMonth - 25`
     daysLeft=`expr 25 + $rest`
  fi
  echo "Mate, it's another $daysLeft days.."
}

# main
_w getDay DAY # call getDay, save the result in the DAY variable
dayzToSalary $DAY

答案 4 :(得分:0)

如果您有一些要传递给函数的参数并想要返回值。 在这里,我将“ 12345”作为参数传递给函数,并在处理完返回变量XYZ后将其分配给VALUE

#!/bin/bash
getValue()
{
    ABC=$1
    XYZ="something"$ABC
    echo $XYZ
}


VALUE=$( getValue "12345" )
echo $VALUE

输出:

something12345