最佳答案
我想从 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