I am trying to run the
echo "Hello world" > foo.txt
using execvp.
So far, I have this:
#include <unistd.h>
int main(void) {
char *execArgs[] = { "echo", "Hello, World! > foo.txt", NULL };
execvp("echo", execArgs);
return 0;
}
which will print out the line "Hello, World! > foo.txt", instead of creating a file named foo.txt with the text "Hello, World!" inside.
Someone does something similar on Stack overflow, they does this:
execlp( "/bin/sh", "/bin/sh", "-c", "cat file1.txt > redirected.txt", (char *)NULL );
But when I changed it to
execlp( "echo", "echo", "echo Hello World > redirected.txt", (char *)NULL );
nothing happens.
EDIT:
Doing this worked (Thanks, SometimesRight!)
execlp( "/bin/sh", "/bin/sh", "-c", "echo Hello, World! > redirected.txt", (char *)NULL );