Yves
Yves

Reputation: 12431

How does the C++ interpret the return value of system()

I need to make my C++ program execute a bash script. So I think I need to use system(), here is an example:

bash script test.sh:

#!/bin/sh
exit 1

C++:

int main()
{
    std::cout << system("./test.sh") << std::endl;
    return 0;
}

To my surprise, I get an 256 etc. Here is what I've found:

 test.sh | C++
---------|-----
 exit 0  | 0
 exit 1  | 256
 exit 2  | 512
...

I don't know why. Is it possible to get the 0, 1, 2 etc.? Does the value depend on the machine?

Upvotes: 2

Views: 3850

Answers (2)

Andre Kampling
Andre Kampling

Reputation: 5630

As said in the comments the return value of system() is implementation defined.
But I assume that you are on Linux where the following holds (see manpage of system()):

Return Value

The value returned is -1 on error (e.g., fork(2) failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status). In case /bin/sh could not be executed, the exit status will be that of a command that does exit(127).

That means to get the exit status of the command use WEXITSTATUS():

std::cout << WEXITSTATUS(system("./test.sh")) << std::endl;

The manpage of wait() explains WEXITSTATUS(), which is defined in the stdlib.h header:

WEXITSTATUS(status)

returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument for a return statement in main(). This macro should only be employed if WIFEXITED returned true.

Upvotes: 1

eerorika
eerorika

Reputation: 238461

Does the value depend on the machine?

Yes (at least as far as C++ standard is concerned).

it possible to get the 0, 1, 2 etc?

C++ standard does not provide a way to get the return value of the sub-process, but POSIX does, which applies to you since you use Linux.

Return Value

If command is a null pointer, system() shall return non-zero to indicate that a command processor is available, or zero if none is available. [CX] [Option Start] The system() function shall always return non-zero when command is NULL. [Option End]

[CX] [Option Start] If command is not a null pointer, system() shall return the termination status of the command language interpreter in the format specified by waitpid(). The termination status shall be as defined for the sh utility; otherwise, the termination status is unspecified. If some error prevents the command language interpreter from executing after the child process is created, the return value from system() shall be as if the command language interpreter had terminated using exit(127) or _exit(127). If a child process cannot be created, or if the termination status for the command language interpreter cannot be obtained, system() shall return -1 and set errno to indicate the error. [Option End]

waitpid() specifies a macro that can be used to extract the value returned from main().

WEXITSTATUS(stat_val)

If the value of WIFEXITED(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main().

Upvotes: 1

Related Questions