bashshellstdoutstderrautosys

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. Below is a simplified example :-

#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

Below is the sample execution output :-

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

That works fine. But when this 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.


Edit 1 :-

#!/usr/bin/env bash

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

function logic() {
    local arg=$1
    local system=$2
    echo "start of logic"
    echo "all params are >$*<"
    echo "perform logic, to get value"
    echo >&2 "logic successfully completed"
    printf -v "$1" '%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 "$@"

Output :-

$

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

Solution

  • Would this work?:

    #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