Return Code of System() = 256

Executing shell script with system() returns 256. What does that mean?

According to this and that, Perl's system() returns exit values multiplied by 256. So it's actually exiting with 1. It seems this happens in C too.

Perl: system return code is 256 instead of 0

From the system function docs:

The return value is the exit status of the program as returned by the wait call. To get the actual exit value, shift right by eight (see below).

The same value is set in $? which documents this further:

This is just the 16-bit status word returned by the traditional Unix wait() system call (or else is made up to look like it). Thus, the exit value of the subprocess is really ($? >> 8), and $? & 127 gives which signal, if any, the process died from, and $? & 128 reports whether there was a core dump.

Also note that either can be set to -1 if there was a fork or exec error (in which case the errno is set in $!).

So this means that when executing your second command, it indicated an exit status of 1 (256 >> 8). I don't know what that means in the context of Windows.

Why does the which system command give a 256 code with os.system in python?

Don't use os.system like that (and don't use which, either). Try this to find a program:

import os

for bin_dir in os.environ.get("PATH").split(":"):
if 'my_program' in os.listdir(bin_dir):
executable_path = os.path.join(bin_dir, 'my_program')
break

Note that this does assume that PATH was properly set by whatever process started the script. If you are running it from a shell, that shouldn't be an issue.

In general, using os.system to call common *NIX utilities and trying to parse the results is unidiomatic-- it's writing python as if it was a shell script.

Then, instead of using system to run pip, use the solution describe in this answer.

Executing command with system() returns 256 times the return value ( 8). What is the point of this?

As far as the language standards are concerned, the return value of system() is implementation-defined. There is no justification there for assuming that the return value of system() has any relationship to the exit status of the command that is run.

With a POSIX-conformant library, however, the return value of a successful call to system() is defined to be in the format used by the wait(2) function. I speculate that this is for consistency, and perhaps also for ease of implementation. In any event, that return value carries information not only about the exit status, but also about how the process terminated -- normally, or because of a signal -- and other information. On such a system, one is expected to check whether the process exited normally by applying the WIFEXITED() macro to the code, and to extract the exit status itself via the WEXITSTATUS() macro. These macros will be declared in sys/wait.h.

That's all very C-ish, of course. There is likely a more C++-ish alternative to some of that.

Why is 256 Returned When Calling A Bash Script Using Python

This is what os.system() returns; on Unix-like systems, the high 8 bits are the result code and the lower 8 are the signal (0 if no signal).

The proper way to do this in Python would be

check = False
with open("/etc/apt/sources.list") as repo:
for line in repo:
if "deb https://http.kali.org/kali kali-rolling main contrib non-free" \
in line or "deb https://http.kali.org/kali kali-rolling main non-free contrib" \
in line:
check = True
break

The proper way to do this in a shell script would be

if grep -q -e "deb https://http\.kali\.org/kali kali-rolling main contrib non-free" -e "deb https://http\.kali\.org/kali kali-rolling main non-free contrib" /etc/apt/sources.list
then ...

(or refactor to use grep -E with a single regex to cover both expressions; see below for an attempt).

If you want to put this in a function, the exit status from grep will be 0 or 1 so there is no need to separately return anything other than that.

The proper way to call this from a Python script would be

import subprocess

check = subprocess.check_call(['grep', '-q', '-E',
r'deb https://http\.kali\.org/kali kali-rolling main( contrib| non-free){2}',
'/etc/apt/sources.list'])


Related Topics



Leave a reply



Submit