X
X
xotkot2014-05-11 20:56:42
linux
xotkot, 2014-05-11 20:56:42

How to write the error stream and return code from one command to variables without using a file?

Let's say that we have a blabla command , and two variables ERROR and KOD
The value of the exit code ($?) of our command (blabla) should always fall into the KOD variable.
If the command (blabla) worked successfully, output the result to the console (&1), in case of failure, write the stream of errors (&2) to the ERROR variable without displaying it.
How can this be done without writing to a file?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
denver, 2014-05-12
@xotkot

Something like this (I write on my knee)
RESULT='blabla 2>&1'
CODE=$?
if [ $CODE -gt 0 ]; then
ERROR=$RESULT
else
echo $RESULT
fi

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question