C程序调用shell脚本共有三种法子 :system()、popen()、exec系列函数
call_exec1.c ,内容为:
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命令以其简略 高效的作用得到很很广泛 的利用 ,下面是一个例子
例:在/tmp/testDir/目录下有shell脚本tsh.sh,内容为
履行 效果 如下:
运行输出
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内容如下:
履行 效果 如下:
运行输出
system() 不用你自己去产生进程,它已经封装了,直接加入自己的命令
exec 需要你自己 fork 进程,然后exec 自己的命令
popen() 也可以实现执行你的命令,比system 开销小
1)system(shell命令或shell脚本路径);
例:在/tmp/testDir/目录下有shell脚本tsh.sh,内容为
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> # !/ bin / sh
wget $ 1
echo " Done! "
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> # !/ bin / sh
wget $ 1
echo " Done! "
在同层目录下新建一个c文件
(以下代码 可带参数)
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> #include < stdio.h >
#include < string .h >
#include < unistd.h >
int main( int argc , char * argv[])
{
char arg[ 300 ] = " /tmp/testDir/tsh.sh " ;
if ( argv[ 1 ] != NULL )
{
strcat(arg,argv[ 1 ]);
system(arg);
printf( " \ndone message in program\n " );
return 1 ;
}
else
{
printf( " Error: Empty input\n " );
return 0 ;
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> #include < stdio.h >
#include < string .h >
#include < unistd.h >
int main( int argc , char * argv[])
{
char arg[ 300 ] = " /tmp/testDir/tsh.sh " ;
if ( argv[ 1 ] != NULL )
{
strcat(arg,argv[ 1 ]);
system(arg);
printf( " \ndone message in program\n " );
return 1 ;
}
else
{
printf( " Error: Empty input\n " );
return 0 ;
}
}
履行 效果 如下:


<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
[root@localhost testDir]#gcc call_exec1.c - o call_exec1
[root@localhost testDir]#. / call_exec1 http: // www.baidu.com/img/logo-yy.gif
-- 2011 - 01 - 21 17 : 02 : 22 -- http: // www.baidu.com/img/logo-yy.gif
正在解析主机 www.baidu.com... 61.135 . 169.105 , 61.135 . 169.125
Connecting to www.baidu.com | 61.135 . 169.105 | : 80 ... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度: 1618 ( 1 .6K) [image / gif]
Saving to: `logo - yy.gif '
100 % [ ======================================> ] 1 , 618 -- . - K / s in 0 .001s
2011 - 01 - 21 17 : 02 : 34 ( 3.05 MB / s) - `logo - yy.gif ' saved [1618/1618]
Done !
done message in program
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
[root@localhost testDir]#gcc call_exec1.c - o call_exec1
[root@localhost testDir]#. / call_exec1 http: // www.baidu.com/img/logo-yy.gif
-- 2011 - 01 - 21 17 : 02 : 22 -- http: // www.baidu.com/img/logo-yy.gif
正在解析主机 www.baidu.com... 61.135 . 169.105 , 61.135 . 169.125
Connecting to www.baidu.com | 61.135 . 169.105 | : 80 ... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度: 1618 ( 1 .6K) [image / gif]
Saving to: `logo - yy.gif '
100 % [ ======================================> ] 1 , 618 -- . - K / s in 0 .001s
2011 - 01 - 21 17 : 02 : 34 ( 3.05 MB / s) - `logo - yy.gif ' saved [1618/1618]
Done !
done message in program
2)popen(char *command,char *type)
例:C程序popentest.c内容如下:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> #include < stdio.h >
main
{
FILE * fp;
charbuffer[ 80 ];
fp = popen(“ ~/ myprogram / test.sh”,”r”);
fgets(buffer, sizeof (buffer),fp);
printf(“ % s”,buffer);
pclose(fp);
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> #include < stdio.h >
main
{
FILE * fp;
charbuffer[ 80 ];
fp = popen(“ ~/ myprogram / test.sh”,”r”);
fgets(buffer, sizeof (buffer),fp);
printf(“ % s”,buffer);
pclose(fp);
}
履行 效果 如下:


<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> #include < stdio.h >
main
{
FILE * fp;
charbuffer[ 80 ];
fp = popen(“ ~/ myprogram / test.sh”,”r”);
fgets(buffer, sizeof (buffer),fp);
printf(“ % s”,buffer);
pclose(fp);
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> #include < stdio.h >
main
{
FILE * fp;
charbuffer[ 80 ];
fp = popen(“ ~/ myprogram / test.sh”,”r”);
fgets(buffer, sizeof (buffer),fp);
printf(“ % s”,buffer);
pclose(fp);
}
from :
http://blog.163.com/redhatroot@126/blog/static/1730563022010102911243458/
作部分修改