7

I have a framework written in python, and for testing purposes I basically want to do a subprocess (aka shell call) ... that should simply come back with a RC != 0. I tried to invoke some non-existing executable; or to run "exit 1"; but those are for some reason translated to a FileNotFoundError.

So, what else could I do to trigger a return code != 0 (in a "reliable" way; meaning the command should not suddenly return 0 at a future point in time).

I thought to "search" for a binary called exit, but well:

> /usr/bin/env exit
/usr/bin/env: exit: No such file or directory
  • 1
    exit 1 is the future-proof way to go. The FileNotFoundError must be caused by something else. – Jos Apr 10 '15 at 13:14
  • When I do "which exit" ... nothing is found. So I assume that "exit" is a bash function; probably not available when I do a subprocess call?! – GhostCat Apr 10 '15 at 13:18
  • Technical nitpicking: a non-zero return code is not intended as an indication of a bad situation, as your question seems to imply. That "0" means "nothing wrong" is just a matter of convention. In fact you have 8 bits of information to convey back to the calling process. – Jos Apr 10 '15 at 13:31
  • @Jos I understand that. But the point is that a RC!=0 leads to a specific exception from subprocess.check_call(). I want to enforce that condition to ensure it is properly handled (to make sure that any RC!=0 situation in real tests is handled as it should be). – GhostCat Apr 10 '15 at 13:35
  • 2
    How about /bin/false? – steeldriver Apr 10 '15 at 13:37
16

If you're looking for a system command that always returns a non-zero exit code, then /bin/false seems like it should work for you. From man false:

NAME
       false - do nothing, unsuccessfully

SYNOPSIS
       false [ignored command line arguments]
       false OPTION

DESCRIPTION
       Exit with a status code indicating failure.
  • No, it does not. At least on Ubuntu 16.04.4 running bash the following command line gives 1 instead of 5: /bin/false 5; echo $? – fviktor Jun 1 '18 at 18:29
  • @fviktor it exits with non-zero status - as the synopsis says, command line arguments (such as 5) are ignored – steeldriver Jun 1 '18 at 18:47
  • You're right. Just no way to control the actual non-zero exit code. – fviktor Jun 2 '18 at 20:20
6

You can create a new return code with the command bash -c "exit RETURNCODE", replacing "RETURNCODE" with any number. Note that it will be trimmed to an 8bit unsigned integer (0...255) by (RETURNCODE mod 256)

You can check the return code of the last shell command inside the terminal(!) with executing echo $?. The "$?" variable contains the most recent return code and "echo" prints it to the standard output.

0

After some more testing, I found that my problem was not on the "Linux" side.

Python has a module shlex; which should be used to "split" command strings. When I changed my subprocess call to use the output of shlex.split() invoking "bash exit 1" gives me what I need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.