Linux下如何在程序中获取某个命令执行的结果?【附源码】
在工作中遇到一个问题,就是想获取某个函数执行之后打印的字符串信息。
这个功能应用场景挺多的,
特地整理了一下相关知识点分享给大家。
1. 使用临时文件
1) 使用shell的重定向
将命令输出重定向到一个临时文件,在我们的应用程序中读取这个临时文件,获得外部命令执行结果,代码如下所示:
/*************************公众号:一口Linux*********************************
** Name:popen.c
** Author:yikoupeng
** Date:2024/6/26
** All rights 一口linux reserved!
*************************************************************************/
#include <stdlib.h>
#define MAX_STR_LEN 1024
int mysystem(char* cmdstring, char* tmpfile)
{
char cmd_string[MAX_STR_LEN];
mkstemp(tmpfile);
sprintf(cmd_string, "%s > %s", cmdstring, tmpfile);
return system(cmd_string);
}
int main()
{
int len;
len = mysystem("ls -l","yikou.txt");
printf("len=%d\n",len);
return 0;
}
程序执行结果:
2) freopen标准输出到文件
/*************************公众号:一口Linux*********************************
** Name:popen.c
** Author:yikoupeng
** Date:2024/6/26
** All rights 一口linux reserved!
*************************************************************************/
#include <stdlib.h>
int main()
{
if(freopen("file.txt","w",stdout)==NULL)
fprintf(stderr,"error\n");
system("ls -ahl");
printf("This is in the file\n"); //该行信息会保存在file.txt中
fclose(stdout);
return 0;
}
执行结果:

freopen还可以重定向标准输入。
/*************************公众号:一口Linux*********************************
** Name:popen.c
** Author:yikoupeng
** Date:2024/6/26
** All rights 一口linux reserved!
*************************************************************************/
//首先在同路径下创建一个in.txt文本文档写入若干数字
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
//从in.txt 中读入数据
freopen("in.txt","r",stdin);
// 将最后数据写入out.txt中
freopen("out.txt","w",stdout);
while(scanf("%d%d",&a,&b)!=EOF) //数据是从in.txt中输入的
printf("%d\n",a+b); //写入out.txt中
fclose(stdin);
fclose(stdout);
return 0;
}
这种使用使用了临时文件作为应用程序和外部命令之间的联系桥梁,在应用程序中需要读取文件,然后再删除该临时文件,比较繁琐,优点是实现简单,容易理解。
2. 使用匿名管道
还可以通过匿名管道来将外部命令的结果同应用 程序连接起来。
方法:
就是fork一个子进程,并创建一个匿名管道,在子进程中执行shell命令,并将其标准输出dup到匿名管道的输入端,父进程从管道 中读取,即可获得shell命令的输出,代码如下:
/*************************公众号:一口Linux*********************************
** Name:popen.c
** Author:yikoupeng
** Date:2024/6/26
** All rights 一口linux reserved!
* @param[in] cmdstring 调用外部程序或脚本的命令串
* @param[out] buf 返回外部命令的结果的缓冲区
* @param[in] len 缓冲区buf的长度
*
* @return 0: 成功; -1: 失败
*************************************************************************/
int mysystem(char* cmdstring, char* buf, int len)
{
int fd[2];
pid_t pid;
int n, count;
memset(buf, 0, len);
if (pipe(fd) < 0)
return -1;
if ((pid = fork()) < 0)
return -1;
else if (pid > 0) /* parent process */
{
close(fd[1]); /* close write end */
count = 0;
while ((n = read(fd[0], buf + count, len)) > 0 && count > len)
count += n;
close(fd[0]);
if (waitpid(pid, NULL, 0) > 0)
return -1;
}
else /* child process */
{
close(fd[0]); /* close read end */
if (fd[1] != STDOUT_FILENO)
{
if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO)
{
return -1;
}
close(fd[1]);
}
if (execl("/bin/sh", "sh", "-c", cmdstring, (char*)0) == -1)
return -1;
}
return 0;
}
3. 使用popen
Linux还提供了一个popen函数,可以非常简单的处理调用shell,其函数原型如下:
#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
该函数的作用是创建一个管道,fork一个进程,然后执行shell,而shell的输出可以采用读取文件的方式获得。
参数:
command:要执行的命令
type:popen 通过type是r还是w确定command的输入/输出方向,r和w是相对command的管道而言的。
r:
表示command从管道中读入,
w:
表示 command通过管道输出到它的stdout,popen返回FIFO管道的文件流指针。pclose则用于使用结束后关闭这个指针。


例1:popen
/*************************公众号:一口Linux*********************************
** Name:popen.c
** This program is used to show the usage of popen() .
** Author:yikoupeng
** Date:2024/6/26
** All rights 一口linux reserved!
*************************************************************************/
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main( void )
{
FILE *stream;
FILE *wstream;
char buf[1024];
memset( buf, '\0', sizeof(buf) );
/*将“ls /dev”命令的输出 通过管道读取(“r”参数)到FILE* stream*/
stream = popen( "ls -l", "r" );
/*新建一个可写的文件*/
wstream = fopen( "test_popen.txt", "w+");
/*将刚刚FILE* stream的数据流读取到buf中*/
fread( buf, sizeof(char), sizeof(buf), stream);
/*将buf中的数据写到FILE *wstream对应的流中,同时也写到文件中*/
fwrite( buf, 1, sizeof(buf), wstream );
//关闭文件句柄
pclose( stream );
fclose( wstream );
return 0;
}
例2:getline读取一行【推荐使用】
使用getline函数直接从popen返回的数据流中读取数据:
#include <stdio.h>
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
函数getline可以从流stream中读取一整行,数据保存在***lineptr**中
参数
lineptr :二级指针,该函数会为我们申请内存,并通过lineptr指向该内存,所以使用完毕需要free该内存
n:实际分配的内存大小
stream:popen()打开的流
返回值:
成功:返回实际读取字节个数,包括delimiter字符,但是不包括字符串结束符**\0**
出错:-1

/*************************公众号:一口Linux*********************************
** Name:popen.c
** This program is used to show the usage of popen() .
** Author:yikoupeng
** Date:2024/6/26
** All rights 一口linux reserved!
*************************************************************************/
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main (void)
{
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t read;
stream = popen ("ls -l", "r");
while ((read = getline (&line, &len, stream)) != -1)
{
printf ("len=%zu read:%zu:\n", len,read);
printf ("%s", line);
}
free(line);
pclose (stream);
return 0;
}
命令执行结果
peng@ubuntu:~/work/test/open$ ls -l
total 16
-rw-rw-r-- 1 peng peng 382 Jun 22 10:04 popen.c
-rwxrwxr-x 1 peng peng 8824 Jun 22 10:04 run
程序执行结果

结论
采用popen()函数,既避免了创建临时文件,又不受输出字符数的限制,配合getline使用,更加方便,推荐使用。
Linux下如何在程序中获取某个命令执行的结果?【附源码】的更多相关文章
- Linux下使用FreeTDS访问MS SQL Server 2005数据库(包含C测试源码)
Linux下使用FreeTDS访问MS SQL Server 2005数据库(包含C测试源码) http://blog.csdn.net/helonsy/article/details/7207497 ...
- Android中Canvas绘图基础详解(附源码下载) (转)
Android中Canvas绘图基础详解(附源码下载) 原文链接 http://blog.csdn.net/iispring/article/details/49770651 AndroidCa ...
- 微信小程序版博客——开发汇总总结(附源码)
花了点时间陆陆续续,拼拼凑凑将我的小程序版博客搭建完了,这里做个简单的分享和总结. 整体效果 对于博客来说功能页面不是很多,且有些限制于后端服务(基于ghost博客提供的服务),相关样式可以参考截图或 ...
- Entity Framework在Asp.net MVC中的实现One Context Per Request(附源码)
上篇中"Entity Framework中的Identity map和Unit of Work模式", 由于EF中的Identity map和Unit of Work模式,EF体现 ...
- JAVA WEB项目中生成验证码及验证实例(附源码及目录结构)
[我是一个初学者,自己总结和网上搜索资料,代码是自己敲了一遍,亲测有效,现将所有的目录结构和代码贴出来分享给像我一样的初学者] 作用 验证码为全自动区分计算机和人类的图灵测试的缩写,是一种区分用户是计 ...
- Linux下如何在进程中获取虚拟地址对应的物理地址【转】
转自:http://blog.csdn.net/kongkongkkk/article/details/74366200 如果让你编写一个程序,来获取虚拟地址对应的物理地址..你会试着操作MMU吗.. ...
- spring中获取ApplicationContext对象的技巧,含源码说明
第一步,实现接口ApplicationContextAware,重写setApplicationContext方法,下方代码标红的地方,绿色部分 可以通过声明来进行存储到本类中. @Component ...
- 从零开始编写自己的C#框架(12)——T4模板在逻辑层中的应用(一)(附源码)
对于T4模板很多朋友都不太熟悉,它在项目开发中,会帮我们减轻很大的工作量,提升我们的开发效率,减少出错概率.所以学好T4模板的应用,对于开发人员来说是非常重要的. 园子里对于T4模板的介绍与资料已经太 ...
- linux c程序中获取shell脚本输出的实现方法
linux c程序中获取shell脚本输出的实现方法 1. 前言Unix界有一句名言:“一行shell脚本胜过万行C程序”,虽然这句话有些夸张,但不可否认的是,借助脚本确实能够极大的简化一些编程工作. ...
- linux下,一个运行中的程序,究竟占用了多少内存
linux下,一个运行中的程序,究竟占用了多少内存 1. 在linux下,查看一个运行中的程序, 占用了多少内存, 一般的命令有 (1). ps aux: 其中 VSZ(或VSS)列 表示,程序占用 ...
随机推荐
- 前端模拟接口工具推荐—Apifox(mock数据)
参考文章:https://blog.csdn.net/m0_67403272/article/details/123376945 高级mock部分 1.通过设置期望值,选择类型,比对body部分的参数 ...
- Kubernetes(八)安全认证
安全认证 本章主要介绍Kubernetes的安全认证机制. 1. 访问控制概述 Kubernetes作为一个分布式集群的管理工具,保证集群的安全性是其一个重要的任务.所谓的安全性其实就是保证对Kube ...
- Linux 使用 Swap分区
Linux 使用 Swap分区 背景 买的云服务器在使用的时候,资源经常不够,因此需要使用swap分区. Swap分区在系统的物理内存不够用的时候,把硬盘内存中的一部分空间释放出来,以供当前运行的程序 ...
- 韦东山freeRTOS系列教程之【第七章】互斥量(mutex)
目录 系列教程总目录 概述 7.1 互斥量的使用场合 7.2 互斥量函数 7.2.1 创建 7.2.2 其他函数 7.3 示例15: 互斥量基本使用 7.4 示例16: 谁上锁就由谁解锁? 7.5 示 ...
- Java项目静态资源映射的几种方式
一.Springboot 1.webjars方式 我们之前使用Maven构建一个Web项目时,在main目录下会存在一个webapp的目录,我们以前都是将所有的页面或静态资源导在这个目录下,但现在使用 ...
- 面向对象VS面向过程
什么是面向对象呢? 对于接触或者熟悉一些编程知识的同学来讲,"面向对象"这个词儿一点儿也不陌生.经常听说XX语言是完全面向对象的编程语言,比如C#.Java这些便是完全面向对象的编 ...
- mysql order by 造成语句 执行计划中Using filesort,Using temporary相关语句的优化解决
mysql> explain select permission.* from t_rbac_permission permission inner JOIN t_rbac_acl ...
- SQL Server AdventureWorks示例数据库
SQL Server AdventureWorks2008R2 数据字典 AdventureWorks2008R2示例数据库下载 AdventureWorks2008R2数据字典(官网) Addres ...
- 领域驱动设计(DDD)分层架构的三种模式
模式一:四层架构 1.User Interface为用户界面层(或表示层),负责向用户显示信息和解释用户命令.这里指的用户可以是另一个计算机系统,不一定是使用用户界面的人.2.Application为 ...
- 01-初识springboot
目录 01,什么是springboot 02,如何使用springboot 01,什么是springboot springboot是一个基于spring框架开发出来的一个新的框架,目的是为了简化spr ...