c - execve: how can I initialise char *argv[ ] with multiple commands instead of a single command? -


execve: how can initialise char *argv[ ] multiple commands instead of single command?
if want execute 4 commands, can use following statement?

char *argv[4][ ] = { {...}, {...}, {...} }; 

and execute them using execve, can use loop var 1 4?

you can not execute multiple commands 1 execve call. in loop need fork program execute multiple execve calls. in manpage of execve it's written:

execve() not return on success, , text, data, bss, , stack of calling process overwritten of program loaded. [...]

return value
on success, execve() not return, on error -1 returned, , errno set appropriately.

method using fork:

output:

hello 1 hello 2 hello 3 

code:

#include <unistd.h> #include <stdio.h>  int main(void) {    int idx;    char *argv[][4] = { {"/bin/sh", "-c", "echo hello 1", 0},                        {"/bin/sh", "-c", "echo hello 2", 0},                        {"/bin/sh", "-c", "echo hello 3", 0} };    (idx = 0; idx < 3; idx++)    {       if (0 == fork()) continue;       execve(argv[idx][0], &argv[idx][0], null);       fprintf(stderr, "oops!\n");    }     return 0; } 

method using command concatenation:
workaround concate commands using shell:

output:

hello 1 hello 2 

code:

#include <unistd.h> #include <stdio.h>  int main(void) {    char *argv[] = {"/bin/sh", "-c", "echo hello 1 && echo hello 2", 0};    execve(argv[0], &argv[0], null);    fprintf(stderr, "oops!\n");     return 0; } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -