M
M
Maxim Kudryavtsev2016-06-24 09:11:56
linux
Maxim Kudryavtsev, 2016-06-24 09:11:56

How to suppress the output of a function while still receiving a return code?

Good day.
I have a custom bash function like this:

superMegaFunction()
{
    .................
    echo "FATAL ERROR: some_text_error"
    return 127
    .................
    return 0
}

Further along the code, you need to get the return code of the function above, i.e. 0 or 127, while suppressing any output like echo. I'm trying to do it like this:
checkMega()
{
    superMegaFunction &> /dev/null
    echo "Status: $?"
}

Execution of the script does not even reach echo, i.e. with the flag set, bash -xI don't see the output " Status: %code% ". I tried the option without superMegaFunction > /dev/null- the result is the same.
Tell me please, what am I doing wrong? With returnbash, after all, in any case, it should return the code without interrupting the execution of the common script, or am I missing something?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xotkot, 2016-06-24
@kumaxim

everything works fine, for example:

f(){
    if 
    then  echo "ok"
          return 0
    else  echo "error"
          return 1
    fi
}

check_f(){
   f &> /dev/null
   echo "Status: $?"

   f ww &> /dev/null
   echo "Status: $?"
}

check_f

the output will be:
Status: 1
Status: 0

you see something in the superMegaFunction function messed up, maybe there is an exit somewhere

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question