How to identify output status in a bash program?

Asked

Viewed 117 times

3

I have a bash that runs a series of scripts in other languages. When I step from one script to another I use the first form of conditional (&&) which I understand as "run the next script if the current one has run successfully" (on these "conditional Runnings" see this question). An example would be:

#usr/bin/bash
python program1.py && python program2.py

Where program2.py wheel only if program1.py run successfully.

It turns out that I would like to print a message in the terminal if any problem occurred in the final script, or along the program (and another message in case of success).

What I thought was to make a parole of the kind:

 #!usr/bin/bash
#variável que gostaria que fosse dada pelo sistema dizendo se o código acima rodou com sucesso
status=$1

if (($status==0))
then
    echo "We finish this step sucessfully"
else
    echo "Some error occurred. Please, check warning and error messages above"
fi

I heard once that in C there is an output status variable. I do not know well, but it is something as if the status variable is equal 0, the program ran successfully, otherwise there was some error. I was wondering if there’s something equivalent in bash.

  • Sorry if I poorly phrased the question title, I accept suggestions to switch so that it is easier to find for users facing the same problem. Is that the concept of output status is not ripe in my head. The real thing is that I don’t even know if this is a Thing

3 answers

3

You can use the $?

Will issue 0:

#!/bin/bash

echo Teste
echo Resultado: $?

It will probably issue 127 because the command foobar failed to exist (or may have other behavior depending on the system):

#!/bin/bash

foobar
echo Resultado: $?

These values can range from 0 to 255, zero being that executed with "success", other values can be anything, or more common errors.

In bash you can also return the status returned by the latest bash "output" program, using the exit

#!/bin/bash

comando-especifico

exit $?

And it can also create conditions for the result:

#!/bin/bash

comando-especifico

if [ $? -eq 0 ]
then
  echo "O programa rodou bem"
else
  echo "O programa falhou"
fi
  • Pq -eq 10? I couldn’t do (($?==0))?

  • 1

    Hello @Lucas I typed wrong (was in a hurry), now this ok, is -ne 0, Ifs no bash work like this: https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

3


Every program called by a shell returns a exit code even if it is not explicit in the code of the program, it returns zero at the end of its execution or anything other than zero in case of failure during its execution.

Explain in your python program a successful output code at the end of the example program:

import sys
from exitstatus import ExitStatus

sys.exit(ExitStatus.success)

Then in your script make things easy using a condition

python script.py
retVal=$?
if [ $retVal -ne 0 ]; then
    echo "Error"
fi

0

Try:

Programa && echo "### ok" || echo "### not ok"

Explanation:

programa && echo "ok"
   se programa retorna 0: 0 && x = 0 (nada mais é executado)
   se programa retorna não 0: não-0 && x = x (executa x)

programa || echo "not ok"
   se programa retorna nao-0: nao-0 || x = não-0 (nada mais é executado)
   se programa retorna 0: 0 || x = x (executa x)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.