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

bpsygsoo  于 2024-01-06  发布在  Linux
关注(0)|答案(1)|浏览(140)

验证码:

  1. function1(){
  2. matchType=$1
  3. if [[ $matchType == 'contains' ]];then
  4. PID=`pgrep -f a`
  5. elif [[ $matchType == 'exact' ]];then
  6. PID=`pgrep -x a`
  7. fi
  8. if [[ $? -eq 0 ]];then
  9. echo "process id fetched successful" // log echo
  10. else
  11. echo"process id fetched failed" // log echo
  12. fi
  13. echo "$PID" //return echo
  14. }
  15. function2()
  16. {
  17. output=$(function1 contains)
  18. }

字符串

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

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

u3r8eeie

u3r8eeie1#

你是说这个吗?

  1. function1(){
  2. matchType=$1
  3. if [[ $matchType == 'contains' ]];then
  4. PID=`pgrep -f a`
  5. elif [[ $matchType == 'exact' ]];then
  6. PID=`pgrep -x a`
  7. fi
  8. if [[ $? -eq 0 ]];then
  9. echo "process id fetched successful" >&2 # this will set stdout fd to stderror
  10. else
  11. echo"process id fetched failed" >&2 # this will set stdout fd to stderror
  12. fi
  13. echo "$PID"
  14. }
  15. function2()
  16. {
  17. output=$(function1 contains)
  18. }

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

展开查看全部

相关问题