GhostCat GhostCat - 1 year ago 174
Linux Question

Generate specific, non-zero return code?

I am working on some piece of python code that calls various linux tools (like ssh) for automation purposes. Right now I am looking into "return code" handling.

Thus: I am looking for a simple way to run some command that gives me a specific non-zero return code; something like

echo "this is a testcommand, that should return with rc=5"

for example. But of course, the above comes back with rc=0.

I know that I can call
, but this will always return with rc=1. I am looking for something that gives me an rc that I can control.

Edit: first answers suggest to exit; but the problem with that: exit is a bash function. So, when I try to run that from within a python script, I get "No such file or directory: exit".

So, I am actually looking for some "binary" tool that gives me that (obviously one can write some simple script to get that; I am just looking if there is something similar to false that is already shipped with any Linux/Unix).

Answer Source

Run exit in a subshell.

$ (exit 5) ; echo $?
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download