C程序调用shell脚本共有三种方法
来源:互联网 发布:数据库日志不可用 编辑:程序博客网 时间:2024/05/20 13:05
C程序调用shell脚本共有三种方法:
1、system() #直接放入shell脚本路径
在c_call_shell.c文件
1#include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5
6 void c_call_shell(char *shell_path)
7 {
8 char str[]="/usr/sxf/MFS/4_25/test/shell.sh";
9 system(shell_path);
10 system(str);
11 }
12
13 int main(int argc,char* argv[])
14 {
15 c_call_shell(argv[1]);
16 }
shell.sh程序
#!/bin/sh
2 #shell.sh
3
4 echo "hello,world"
2、popen(char *command,char *type)
type为r或w
command 为shell脚本路径
popen()会调用fork()产生子进程,子进程调用/bin/sh -c来执行参数command指令。参数type可使用“r”代表读,“w”代表写。
popen()会建立管道连到子进程的标准输出设备或标准输入设备,然后返回一个文件指针,所有对文件指针的操作都可以用,除了fclose()外。
c_print_shell.c
#include <stdio.h>
2 int main(int argc,char *argv[])
3 {
4 FILE *fp=NULL;
5 char buffer[80];
6 fp=popen("/usr/sxf/MFS/4_25/test/shell.sh","r");
7 fgets(buffer,sizeof(buffer),fp);
8 printf("%s\n",buffer);
9 pclose(fp);
10
11 return 0;
12 }
shell.sh
#!/bin/sh
2 #shell.sh
3
4 echo "hello,world"
- 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脚本的三种方式
- 旋转图片的几种方式笔记
- map 组织新数组格式
- pycharm安装mysql-python报错
- ionic3 地区选择器
- c++对象的序列化与反序列化的解决方案----flatbuffers的使用
- C程序调用shell脚本共有三种方法
- C语言中自带头文件(.h)中包含的函数
- 查找------红黑树
- Python常见文件操作的函数示例
- ognl和struts2标签_个人学习日志
- Android WebView的基本使用
- Linux 文件的复制,移动(剪切、改名)和删除(强制删除)
- 数组,以及数组的初始化
- 设置对话框、static和group的背景色和字体颜色