simplify function call that writes to output stream and returns value

不羁岁月 提交于 2020-07-10 07:08:35

问题


This is a follow up question for write to output stream and returning value from shell script function.

In shell script, echo can be used to return values from functions. But, if those functions want to print some messages.

Then, this can be done by redirecting it to error stream.

But when the script is used as a autosys job. then messages from logic functions ends up in error stream file triggering alerts.

Is there any way, messages from logic function can be written to output stream and not mixing messages with return value.

Below is the solution suggested by James Brown :-

#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 >&2 "logic done"

    echo "ok" >&4
}

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

    echo "result is >$result<"

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

    echo >&2 "end of smain"
}

smain

Now logic function is called at many places in our scripts. Above syntax is little too much too call logic. So, I tried adding helper functions(Return and sendReturnValue) to simplify this, but it's not working as expected.

Version 1

function sendReturnValue(){
  echo "$*" >&4
}

function Return(){
  { local result=$( { { sendReturnValue "$*" ; } 1>&3 ; } 4>&1); } 3>&1
  echo "$result"
}

#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 >&2 "logic done"

    Return "ok"
}

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

    local result=$(logic)
    echo "result is >$result<"

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

    echo >&2 "end of smain"
}

smain

Version 2

Added getValue function :-

function getValue() {
    local fun=$1
    { local result=$({ { $fun; } 1>&3; } 4>&1); } 3>&1
    echo "$result"
}

#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 >&2 "logic done"

    echo "ok" >&4
}

function smain() {
    #{ local result=$({ { logic; } 1>&3; } 4>&1); } 3>&1
    local result=$(getValue "logic")

    echo "result is >$result<"

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

    echo >&2 "end of smain"
}

smain

Actual output of above two versions is same :-

$
sh returnValueWithEcho.sh > out.log 2>err.log

$
cat err.log
logic done
end of smain

$
cat out.log
result is >start of logic
perform logic, to get value
ok<
script failed

Expected output :-

$
sh returnValueWithEcho.sh > out.log 2>err.log

$
cat err.log
logic done
end of smain

$
cat out.log
start of logic
perform logic, to get value
result is >ok<
script successful

来源:https://stackoverflow.com/questions/62739537/simplify-function-call-that-writes-to-output-stream-and-returns-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!