In my program I\'m executing given command and getting result (log, and exit status). Also my program have to support shell specific commands (i.e. commands which contains shell
execvp
takes a path to an executable, and arguments with which to launch that executable. It doesn't take bourne shell commands.
ls | wc
is a bourne shell command (among others), and it can't be broken down into the path to an executable and some arguments due to the use of a pipe. This means it can't be executed using execvp
.
To execute a bourne shell command using execvp
, one has to execute sh
and pass -c
and the command for arguments.
So you want to execute ls | wc
using execvp
.
char *const argv[] = {
"sh",
"-c", "ls | wc", // Command to execute.
NULL
};
execvp(argv[0], argv)
You apparently tried
char *const argv[] = {
"sh",
"-c", "ls", // Command to execute.
"|", // Stored in called sh's $0.
"wc", // Stored in called sh's $1.
NULL
};
That would be the same as bourne shell command sh -c ls '|' wc
.
And both are very different than shell command sh -c ls | wc
. That would be
char *const argv[] = {
"sh",
"-c", "sh -c ls | wc", // Command to execute.
NULL
};
You seem to think |
and wc
are passed to the sh
, but that's not the case at all. |
is a special character which results in a pipe, not an argument.
As for the exit code,
Bits 15-8 = Exit code.
Bit 7 = 1 if a core dump was produced.
Bits 6-0 = Signal number that killed the process.
32512 = 0x7F00
So it didn't die from a signal, a core dump wasn't produced, and it exited with code 127 (0x7F).
What 127 means is unclear, which is why it should accompanied by an error message. You tried to execute program ls | wc
, but there is no such program.