我想从外壳程序脚本中调用的函数返回值。也许我缺少语法。我尝试使用全局变量。但这也不起作用。代码是:
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
与您的问题无关,但是无论如何...如果您试图获取锁,则可以使用“ lockfile”命令。
—
维克托·赫雷兹(VíctorHerraiz)2014年