写入输出流并从 shell 脚本函数返回值

write to output stream and returning value from shell script function

在shell 脚本中,echo 可用于return 来自函数的值。 但是,如果这些函数想要打印一些消息。 然后,这可以通过将其重定向到错误流来完成。 下面是一个简化的例子:-

#this is a function that returns a value, as well as
#print some messages
function logic(){
    echo >&2 "start of logic"
    echo >&2 "perform logic, to get value"

    echo "ok"
}

function smain(){
    local result=$(logic)

    echo "result is >$result<"

    if [ "$result" == "ok" ];then
        echo "script successful"
    else
        echo "script failed"
    fi
}

smain

下面是示例执行输出:-

sh sample.sh
start of logic
perform logic, to get value
result is >ok<
script successful

效果很好。 但是当这个脚本被用作 autosys 作业时。 然后来自逻辑函数的消息最终在错误流文件中触发警报。

有什么方法可以将来自逻辑函数的消息写入输出流,而不是将消息与 return 值混合。


编辑 1 :-

#!/usr/bin/env bash

function Return(){
    printf -v "" '%s' '$*'
}

function logic() {
    local arg=
    local system=
    echo "start of logic"
    echo "all params are >$*<"
    echo "perform logic, to get value"
    echo >&2 "logic successfully completed"
    printf -v "" '%s' 'abraKaDabra'
}

function main() {
    local result='' ;  logic "$@" result

    printf 'result is >%s<\n' "$result"

    if [ "$result" = "ok" ]; then
        echo "script successful"
    else
        echo "script failed"
    fi

    echo >&2 "end of main"
}

main "$@"

输出:-

$

sh returnValueWithDebugging.sh abc xyz > out.log 2>err.log

$

cat err.log
logic successfully completed
end of main

$

cat out.log
start of logic
all params are >abc xyz result<
perform logic, to get value
result is ><
script failed

这行得通吗?:

#this is a function that returns a value, as well as                            
#print some messages                                                            
function logic(){
    echo "start of logic"
    echo "perform logic, to get value"

    echo "ok" >&2
}

function smain(){
    { local result=$( { { logic ; } 1>&3 ; } 2>&1); } 3>&1

    echo "result is >$result<"

    if [ "$result" == "ok" ];then
        echo "script successful"
    else
        echo "script failed"
    fi
}

smain

如果函数的 return 是一个简单的状态指示器(例如 ok 或 fail),我建议您使用 return 代码。

并且由于您将问题标记为 Bash,return 可以通过引用而不是使用输出流来实现任意值,其优点是不在子 shell 进程:

传递 return 值作为参考(结果变量名称作为参数 1 传递):

#!/usr/bin/env bash
logic()
{
  echo "start of logic"
  echo "perform logic, to get value"
  printf -v "" '%s' 'ok'
}

function main ()
{
  result=''
  logic result

  printf 'result is >%s<\n' "$result"

  if [ "$result" = "ok" ];then
    echo "script successful"
  else
    echo "script failed"
  fi
}

main "$@"

或者,您可以将 name-ref 变量与 declare -n 一起使用,并将 logic 函数编写为:

logic()
{
  local -n res=""
  echo "start of logic"
  echo "perform logic, to get value"
  res='ok'
}

logic 使用的附加参数的另一个示例:

#!/usr/bin/env bash
logic()
{
  local -n res=""
  local a=""
  local b=""
  echo "start of logic"
  echo "perform logic, to get value"
  if [ $a -gt $b ]; then
    res='ok'
  else
    res='oh no!'
  fi
}

function main ()
{
  result=''
  logic result 3 2

  printf 'result is >%s<\n' "$result"

  if [ "$result" = "ok" ];then
    echo "script successful"
  else
    echo "script failed"
  fi

  logic result 5 10

  printf 'result is >%s<\n' "$result"
}

main "$@"