- 38
- 0
- 约4.45千字
- 约 9页
- 2017-08-03 发布于河南
- 举报
新linux c程序中获取shell脚本输出(Get the shell script output in the new linux c program)
1. Preface
There is a famous saying in the Unix community: one line of shell scripts is better than a thousand lines of C programs. Although this sentence is somewhat exaggerated, it can not be denied that it can greatly simplify some programming work with the aid of the script. For example, to implement a ping program to test the connectivity of the network, and to implement the ping function, you need to write the 200~300 line code. Why cant you call the ping command of the system directly? Usually in a program, the shell command is invoked through the system function. However, the system function only returns the command if it succeeds, and we may need to get the output of the shell command on the console. For example, after execution of the external command Ping, if the execution fails, we want to get the return information of ping.
2. use temporary files
The first thought is to redirect the command output to a temporary file, read the temporary file in our application, and get the external command execution results, as shown in the following code:
#define CMD_STR_LEN 1024
Int mysystem (char*, cmdstring, char*, tmpfile)
{
Char cmd_string[CMD_STR_LEN];
Tmpnam (tmpfile);
Sprintf (cmd_string,%s %s, cmdstring, tmpfile);
Return system (cmd_string);
}
The use of temporary files as between the application and the external command link, you need to read a file in your application, then delete the temporary files, more complicated, it is easy to implement, easy to understand. Is there any way without resorting to temporary documents?
3. using anonymous pipes
In the environment of UNIX advanced programming book gives a way through the anonymous pipe program output to the paging procedure example, so think of, we can also be external command results with applications connected by pipeline. Fork method is a sub process, and create an anonymous pipeline, execute the shell command in the sub proces
您可能关注的文档
- 数据库挂号(Database registration).doc
- 数据库的考题(Database questions).doc
- 数据库例题(Database example).doc
- 手诊讲座(Department of hand).doc
- 数据库紧急恢复过程(Database emergency recovery process).doc
- 数据库连接的五要素(Five elements of a database connection).doc
- 数据库性能调优(Database performance tuning).doc
- 数据库语句习题(Database statement exercises).doc
- 数据库原理例题(Examples of Database Principles).doc
- 数据挖掘和BI(Data mining and BI).doc
- 新奥拓的磨合(The new Alto running).doc
- 新奥法是一种把设计、施工、监测相结合的科学的隧道建造方法。(NATM is a scientific method of tunnel construction combining design, construction and monitoring.).doc
- 新版中日交流标准日本语·学习攻略§+第一单元小结(The new Sino Japanese standard, learning strategy, the first unit + summary).doc
- 新标准日语二级(N2)自动词与他动词列表(全-共45个)(New standard Japanese two (N2) automatic words and their verbs list (all - 45)).doc
- 新标准小学一年级上册英语句型练习-兔笨笨英语网(The new standard of the first grade primary school English drills on rabbit stupid English Network).doc
- 新材料在建筑节能中的应用及质量问题探析(Application and quality analysis of new material in building energy saving).doc
- 新产品的概念与特点(Concept and characteristics of new product).doc
- 新财富―让城市更美好管理(New wealth - Better City Management).doc
- 新产品开发程序(New product development process).doc
- 新产品研发流程(New product development process).doc
最近下载
- 光宝ISA-7X标准泛用型伺服系统技术手册.pdf
- 小学四年级奥数《排列与组合》基础知识讲解+专项练习题+参考答案.docx
- 《0~3岁婴幼儿心理发展与教育》项目三 婴幼儿认知发展 教学课件.pptx VIP
- T_GDSBME 001-2026(儿童标准化可弯曲支气管镜诊疗中心建设).pdf
- 中考历史【选择题】专练(8大题型+80道预测题).docx VIP
- 中医诊断学的病案分析.ppt VIP
- 2025年信息系统安全专家漏洞修复的回滚计划与应急预案专题试卷及解析.pdf VIP
- 2025年招标师招标采购合同违约责任认定与承担方式纠纷专题试卷及解析.pdf VIP
- 能源计量数据可信验证中的边缘-云协同架构.pdf VIP
- 2025年演出经纪人艺人推广中口碑营销与舆论引导策略专题试卷及解析.pdf VIP
原创力文档

文档评论(0)