system(" ");

popen

exec 系列

execl等等类似的函数都可以执行任何shell下的命令。

#include

int

main(){

system("ls -l");

return 1;

}

或者

int execl(const char *path, const char *arg, ...);

int execlp(const char *file, const char *arg, ...);

int execle(const char *path, const char *arg , ..., char * const envp[]);

int execv(const char *path, char *const argv[]);

int execvp(const char *file, char *const argv[]);

也可以,不同之处是前者可以执行完毕而不退出,后者执行完毕,正确会推出,错误则不退出,继续下面的语句。

linux下如何用c语言调用shell命令   C程序调用shell脚本共有三种方法 C程序调用shell脚本共有三种法子 :system()、popen()、exec系列函数 system() 不用你自己去产生进程,它已经封装了,直接加入自己的命令 exec 需要你自己 fork 进程,然后exec 自己的命令 popen() 也可以实现执行你的命令,比system 开销小 1)system(shell命令或shell脚本路径);        system()会调用fork()产生 子历程,由子历程来调用/bin/sh-c string来履行 参数string字符串所代表的命令,此命令履行 完后随即返回原调用的历程。在调用system()期间SIGCHLD 信号会被暂时搁置,SIGINT和SIGQUIT 信号则会被漠视 。         返回值:如果system()在调用/bin/sh时失败则返回127,其他失败原因返回-1。若参数string为空指针(NULL),则返回非零值。 如果 system()调用成功 则最后会返回履行 shell命令后的返回值,但是此返回值也有可能为system()调用/bin/sh失败所返回的127,因 此最好能再反省 errno 来确认履行 成功 。      system命令以其简略 高效的作用得到很很广泛 的利用 ,下面是一个例子 例:在~/test/目录下有shell脚本test.sh,内容为 #!bin/bash #test.sh echo hello   在同层目录下新建一个c文件system_test.c,内容为:   #include int main() {   system("~/test/test.sh"); }     履行 效果 如下:   [root@localhost test]$gcc system_test.c -o system_test  [root@localhost test]$./system_test hello [root@localhost test]$ 2)popen(char *command,char *type)          popen()会调用fork()产生 子历程,然后从子历程中调用/bin/sh -c来履行 参数command的指令。参数type可应用 “r”代表读取,“w”代表写入。遵循此type值,popen()会建立 管道连到子历程的标准 输出设备 或标准 输入设备 ,然后返回一个文件指针。随后历程便可利用 此文件指针来读取子历程的输出设备 或是写入到子历程的标准 输入设备 中。此外,所有应用 文 件指针(FILE*)操作的函数也都可以应用 ,除了fclose()以外。       返回值:若成功 则返回文件指针,否则返回NULL,差错 原因存于errno中。注意:在编写具SUID/SGID权限的程序时请尽量避免应用 popen(),popen()会继承环境变量,通过环境变量可能会造成系统安全的问题。   例:C程序popentest.c内容如下:     #include     main     {         FILE * fp;         charbuffer[80];         fp=popen(“~/myprogram/test.sh”,”r”);         fgets(buffer,sizeof(buffer),fp);         printf(“%s”,buffer);         pclose(fp);     }     履行 效果 如下:   [root@localhost test]$ vim popentest.c [root@localhost test]$ gcc popentest.c -o popentest [root@localhost test]$ ./popentest /root/test [root@localhost test]$ #include     int   system(const   char   *string); 例:在~/myprogram/目录下有shell脚本test.sh,内容为    #!bin/bash    #test.sh    echo $HOME    在该目录下新建一个c文件systemtest.c,内容为:    #include    /*This program is used to test function system*/    main()    {    system("~/myprogram/test.sh");    }    执行结果如下:    xiakeyou@ubuntu:~/myprogram$ gcc systemtest.c -o systemtest    xiakeyou@ubuntu:~/myprogram$ ./systemtest    /home/d/e/xiakeyou    xiakeyou@ubuntu:~/myprogram$    2)popen(char *command,char *type)    执行过程:popen()会调用fork()产生子进程,然后从子进程中调用/bin/sh -c来执行参数command的指令。参数type可使用“r”代表读取,“w”代表写入。依照此type值,popen()会建立管道连到子进程的标准输出设备或标准输入设备,然后返回一个文件指针。随后进程便可利用此文件指针来读取子进程的输出设备或是写入到子进程的标准输入设备中。此外,所有使用文件指针(FILE*)操作的函数也都可以使用,除了fclose()以外。    返回值:若成功则返回文件指针,否则返回NULL,错误原因存于errno中。    注意:在编写具SUID/SGID权限的程序时请尽量避免使用popen(),popen()会继承环境变量,通过环境变量可能会造成系统安全的问题。    例:C程序popentest.c内容如下:    #include    main()    {    FILE * fp;    charbuffer[80];    fp=popen(“~/myprogram/test.sh”,”r”);    fgets(buffer,sizeof(buffer),fp);    printf(“%s”,buffer);    pclose(fp);    }    执行结果如下:    xiakeyou@ubuntu:~/myprogram$ vim popentest.c    xiakeyou@ubuntu:~/myprogram$ gcc popentest.c -o popentest    xiakeyou@ubuntu:~/myprogram$ ./popentest    /home/d/e/xiakeyou    xiakeyou@ubuntu:~/myprogram$    只是偶能力可能有点有限,没有太看懂。直接用system()倒是脚本可是执行,只是返回值却是一塌糊涂,试了多次也没有找到什么规律。不免又看了一下上面的那篇博文,得到一些启发,可以这样来实现:    先将脚本的返回值利用 echo > XXXXX 输出到一个本地文件中    当需要这个返回值是,可是通过C语言的文件操作函数来直接从文件中读取    后来一想,这应该就是上文中POPEN的实现方法!  --------------------------------------------------------------------------------------------------------------

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐