i am trying to perform the command sh that start a new terminal in Linux from a c program, but i get this error /bin/sh: /bin/sh: cannot execute binary file when i am using execv ()and same result with fork and then execv. but when i tried it system command a succeed , how can i do this command with execv?
work's :
int main (){
system("sh");
return 0;
}
does'nt work :
int main (){
int ret=0;
char *argv[] = {"/bin/bash", "/bin/sh",NULL};
ret=execv(argv[0], argv);
printf("ret: %d \n",ret);
return 0;
}
this code return this error : /bin/sh: /bin/sh: cannot execute binary file
"/bin/bash","-c","/bin/sh"
/bin/bash
/bin/bash /bin/sh
, and if you run that at the command line you'll get the exact same error. The reason is that by default, /bin/bash interprets its first argument as a shell script file to be executed, and /bin/sh is not a shell script, it's a binary.