LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   run shell command inside of c code? (https://www.linuxquestions.org/questions/programming-9/run-shell-command-inside-of-c-code-182323/)

khucinx 05-17-2004 02:04 AM

run shell command inside of c code?
 
in one directory, i have cpc.c and cource file

cpc.c is :
#include<stdio.h>
#include<stdlib.h>
main()
{
system("/bin/sh -c cp source dest");
}

i want copy source file to dest file, but there is no dest file.
when i execute cpc by type ./cpc, there is something error like "cp: missing file arguments".

how about that?

thank's

pragti 05-17-2004 02:24 AM

Hi
The correct prog is following
cpc.c is :
#include<stdio.h>
#include<stdlib.h>
main()
{
system("/bin/sh \"-c cp source dest\"");
}

as -c option take the single input string as argument in shell.so multiple strings must be quoted

Hko 05-17-2004 10:04 AM

Why so difficult?
According to "man system":
Quote:

system() executes a command specified in string by calling /bin/sh -c string, and returns after the command has been completed.
So if system() already will start "sh -c" for you, why start another one?
Code:

#include <stdlib.h>

int main()
{
        system("cp source dest");
        return 0;
}



All times are GMT -5. The time now is 12:06 AM.