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 false
, 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).
Run exit
in a subshell.
$ (exit 5) ; echo $?
5
This is not exactly what you are asking but custom rc
can be achieved through exit command.
echo "this is a test command, that should return with " ;exit 5
echo $?
5
I have this function defined in .bashrc
:
return_errorcode ()
{
return $1
}
So, I can directly use something like
$ return_errorcode 5
$ echo $?
5
Compared to (exit 5); echo $?
(exit 5); echo $?
option, this mechanism saves you a subshell.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.