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

我想从 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
458309 次浏览

Bash 函数不能直接返回字符串,你可以做三件事:

  1. 回声一串
  2. 返回一个退出状态,它是一个数字,而不是一个字符串
  3. 共享一个变量

对于其他一些 shell 也是如此。

下面是如何做到这些选项中的每一个:

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

如果它只是一个 true/false 测试,那么函数 return 0表示成功,return 1表示失败。接下来的测试将是:

if function_name; then
do something
else
error condition
fi

你工作太辛苦了,你的整个剧本应该是:

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

但即使这样也可能太冗长了,我会编写代码:

mkdir "$lockdir" || exit 1

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

我认为返回0表示 Succ/1表示 false (Glenn Jackman) ,而 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

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

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




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

产出:

something12345