linux 函数中有log echo和return echo,但它只返回log echo only值,而不是return cho

bpsygsoo  于 12个月前  发布在  Linux
关注(0)|答案(1)|浏览(111)

验证码:

function1(){

matchType=$1

if [[ $matchType == 'contains' ]];then
  PID=`pgrep -f  a`
elif [[ $matchType == 'exact' ]];then
  PID=`pgrep -x  a`
fi

if [[ $? -eq 0 ]];then
 echo "process id fetched successful"  // log echo
else
 echo"process id fetched failed"  // log echo
fi

echo "$PID"  //return echo

}

function2()
{
output=$(function1 contains)
}

字符串

output变量的值是'process id fetched failed',而不是我在函数1中最后回显的PID

要求:它应该返回PID值到函数2

u3r8eeie

u3r8eeie1#

你是说这个吗?

function1(){

matchType=$1

if [[ $matchType == 'contains' ]];then
  PID=`pgrep -f  a`
elif [[ $matchType == 'exact' ]];then
  PID=`pgrep -x  a`
fi

if [[ $? -eq 0 ]];then
 echo "process id fetched successful" >&2 # this will set stdout fd to stderror
else
 echo"process id fetched failed" >&2  # this will set stdout fd to stderror
fi

echo "$PID"

}

function2()
{
output=$(function1 contains)
}

字符串
或者您要在何处记录?

相关问题