C程序调用shell脚本共有三种方法
来源:互联网 发布:jdk下载32位 linux 编辑:程序博客网 时间:2024/05/09 17:12
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,内容为
#!/bin/sh
wget $1
echo "Done!"
wget $1
echo "Done!"
在同层目录下新建一个c文件
(以下代码 可带参数)
#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;
}
}
#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;
}
}
履行 效果 如下:
运行输出
[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
[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内容如下:
#include<stdio.h>
main
{
FILE * fp;
charbuffer[80];
fp=popen(“~/myprogram/test.sh”,”r”);
fgets(buffer,sizeof(buffer),fp);
printf(“%s”,buffer);
pclose(fp);
}
main
{
FILE * fp;
charbuffer[80];
fp=popen(“~/myprogram/test.sh”,”r”);
fgets(buffer,sizeof(buffer),fp);
printf(“%s”,buffer);
pclose(fp);
}
履行 效果 如下:
运行输出
#include<stdio.h>
main
{
FILE * fp;
charbuffer[80];
fp=popen(“~/myprogram/test.sh”,”r”);
fgets(buffer,sizeof(buffer),fp);
printf(“%s”,buffer);
pclose(fp);
}
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/
作部分修改来源:http://www.cnblogs.com/no7dw/archive/2011/01/22/1941858.html
0 0
- C程序调用shell脚本共有三种方法
- C程序调用shell脚本共有三种方法
- C程序调用shell脚本共有三种方法
- C程序调用shell脚本共有三种方法
- C程序调用shell脚本共有三种方法
- C程序调用shell脚本共有三种方法
- C程序调用shell脚本共有三种方法
- C程序调用shell脚本共有三种方法
- C程序调用shell脚本共有三种方法
- C程序调用shell脚本共有三种方法
- C程序调用shell脚本共有三种法子
- C程序调用shell脚本共有三种方式:system()、popen()、exec系列函数
- C程序调用shell脚本的三种方法
- shell脚本 调用 另一个 shell脚本的三种方法
- 三种shell脚本调用方法
- C程序调用shell脚本
- c程序调用shell脚本获取返回值方法
- c语言中调用shell脚本的三种方式
- [emerg]: getpwnam(“nginx”) failed
- 网站推广天天都不能忽略的工作-功成网络
- MySQL性能优化的21个最佳实践 和 mysql使用索引
- 循环列表的Java实现,解决约瑟夫环问题
- 大数据给嵌入式系统带来新的发展机遇
- C程序调用shell脚本共有三种方法
- Cloneable
- 博客搭建步骤
- ListView自动加载
- 【Android】Eclipse自动编译NDK/JNI的三种方法
- 第十二章 传感器
- 上线流程
- iOS开发必备技能 - 自动布局在开发中的应用
- SIP协议详解