在管道中获取命令输出,C for Linux

2024-03-22

我需要运行 Linux CLI 命令并从 C 获取其标准输出。

我可以使用 pipeline() 创建一个管道,然后 fork/exec,在调用 exec() 之前将子级的标准输出描述符重定向到管道,并从父级中的管道读取。另外我还得伺候孩子。

是否有一个简单的调用来执行 fork + 重定向 + exec + wait,就像 system() 执行 fork + exec + wait 一样,只有 system() 不执行重定向。

有popen(),它执行fork+redirect+exec,但不执行等待,所以我无法获得退出状态。


是这个吗?



NAME
       popen, pclose - process I/O

SYNOPSIS
       #include <stdio.h>  

       FILE *popen(const char *command, const char *type);

       int pclose(FILE *stream);

DESCRIPTION
       The  popen()  function opens a process by creating a pipe, forking, 
and invoking the shell.  Since a pipe is by definition unidirectional, the 
type argument may specify only reading or writing, not both; the resulting 
stream is correspondingly read-only or write-only.

       The command argument is a pointer to a null-terminated string 
containing a shell command line.  This command is passed to /bin/sh 
using the -c flag; interpretation, if any, is performed by the shell.  
The type argument is a pointer to a null-terminated string which must be 
either ‘r’ for reading or ‘w’ for writing.

       The  return  value  from popen() is a normal standard I/O stream in 
all respects save that it must be closed with pclose() rather than fclose().  
Writing to such a stream writes to the standard input of the command; the 
command’s standard output is the same as that of the process that called 
popen(), unless this is altered by the command itself.  Conversely, reading 
from a ‘‘popened’’ stream reads the command’s standard output, and the 
command’s standard input is the same as that of the process that called 
popen().

       Note that output popen() streams are fully buffered by default.

       The pclose() function waits for the associated process to terminate 
and returns the exit status of the command as returned by wait4().
  
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在管道中获取命令输出,C for Linux 的相关文章

  • Microsoft Graph API 授权错误:无效受众

    我知道这是一个很长的问题 但如果有人能与我分享他们的想法或经验 我真的很感激 因为我已经解决这个问题几天了 现在正在尝试很多事情 我有一个 ASP Net Core 3 1 Web API 应用程序和一个 ASP NET Core 3 1
  • 查找系统日志最大消息长度

    大多数 Unix 程序员都会习惯由syslog h 并且许多实现 例如 glibc 对发送给它的 syslog 消息的大小没有真正的限制 但通常对侦听的应用程序有限制 dev log 我想知道是否有人知道如何找到系统日志的最大消息大小 或者
  • 如何让我的方法等待所有线程完成?

    我有一个方法可以触发线程来完成一些工作 将有 2 个线程异步运行一段时间 当调用它们的回调方法时 回调会触发另一个线程 直到所有工作完成 如何让我的方法等待所有这些线程完成并被触发 如果这是 Net 4 0 您可以使用CountdownEv
  • 如何更改 GridView 内 ListViewItemPresenter 中的 SelectedBackground

    我在 SubSection 中有一个 Clickable Gridview
  • 正确别名向量

    我无法在其他地方找到答案 所以我想我只需要问这个 我正在尝试获取向量 其中存储 int 指针 的别名 如下所示 void conversion Engine ENGINES The Engine class has a vector of
  • 如何将 list 对象附加到另一个对象

    在 C 中 我有两个list
  • 无法使用 MinGW C++ 在 NetBeans IDE 7.3 中编译“Hello World”

    我正在尝试制作一个简单的 Hello World 在 NetBeans IDE 7 3 中使用 MinGW 作为我的 C 编译器来编写程序 我遇到了构建失败的情况 但我不知道为什么 这是我的编译器设置 Family MinGW 基本目录 C
  • 在 Asp.net Web API 中处理 CORS 预检

    我的架构中有三个应用程序 它们位于同一服务器上 但具有不同的端口号 A Token Application port 4444 Asp net WebApi B API Application port 3333 Asp net WebAp
  • Clang 使用 -nostdlib 生成崩溃代码

    我正在尝试为可执行文件设置自己的运行时环境 但无法使用 clang v3 4 1ubuntu1 目标 x86 64 pc linux gnu 来生成没有段错误的可执行文件 我已将问题简化为以下内容 如果我有一个文件 crt1 c 除了满足
  • 使用Linux虚拟鼠标驱动

    我正在尝试实施一个虚拟鼠标驱动程序根据基本 Linux 设备驱动程序书 有一个用户空间应用程序 它生成坐标以及内核模块 See 虚拟鼠标驱动程序和用户空间应用程序代码 http www embeddedlinux org cn Essent
  • Windows Phone 8.1 应用程序多语言

    我正在使用 Visual Studio 2015 在 SilverLight 中创建 Windows Phone 应用程序 8 1 我正在用英语和阿拉伯语创建多语言应用程序 为此 我在项目中创建了 Strings 文件夹 其中包含 en U
  • 实体框架..自引用表..获取深度=x的记录?

    我成功地在实体框架中使用自引用表 但我不知道如何获得所需深度的记录 这应该是什么逻辑 Model public class FamilyLabel public FamilyLabel this Children new Collectio
  • 拦截C# HttpClient GetAsync

    我有一个 Web 项目 C MVC5 但没有 WebAPI 和一个简单的 HTTP REST 客户端 该客户端调用外部 REST 服务并获取 accessToken 等 我想检查所有 Get PostAsync 调用对 statusCode
  • C# SerialPort BaseStream ReadAsync - CancellationToken 从未取消?

    我尝试以异步方式从串行端口读取数据 请记住操作所花费的时间不得超过指定的时间段 我使用的代码 private async Task
  • 获取当前时间(以小时和分钟为单位)

    我正在尝试从系统收集信息 并且需要获取当前时间 以小时和分钟为单位 目前我有 date awk print 4 输出如下 16 18 54 怎样才能把秒数去掉呢 提供格式字符串 date H M Running man date将给出所有格
  • Rails:使用 RestClient 的外部 API 集成(未定义的局部变量或方法“user”)

    我正在建设一个数字图书馆 我已经完成了很多需要的功能 我目前在将数字图书馆与学习管理系统 LMS 集成时遇到问题 我已经有一个数字图书馆的管理员身份验证系统 使用设计宝石 我的目标是允许想要访问数字图书馆的用户使用其学习管理系统 LMS 凭
  • 对 MFC UI 应用程序进行单元测试吗?

    如何对大型 MFC UI 应用程序进行单元测试 我们有一些大型 MFC 应用程序已经开发了很多年 我们使用一些标准的自动化 QA 工具来运行基本脚本来检查基础知识 文件打开等 这些由 QA 小组在日常构建后运行 但我们希望引入一些程序 以便
  • 虚拟键盘(类似 Swype 键盘)Windows 窗体应用程序 C#

    我正在尝试使用 c 在 Windows 窗体中创建一个类似 swype 的键盘 我有两个问题 A 我无法重现手指滑动动作 b 我无法识别不同按键下的字母 对于第一个问题 我使用了 Graphics 类和 Pen 类 并像这样使用它 bool
  • 在.Net中使用ObjectCache缓存对象并设置过期时间

    我陷入了一个场景 我的代码如下 更新 它不是关于如何使用数据缓存 我已经在使用它及其工作 它是关于扩展它 以便该方法在到期时间和从外部源获取新数据之间不会进行调用 object string this GetDataFromCache ca
  • 如何为单个函数设置 ICC 属性“fp-model precision”,以防止关联优化?

    我正在实施卡汉求和 http en wikipedia org wiki Kahan summation algorithm 在支持 gcc47 gcc48 clang33 icc13 和 icc14 编译的项目中 作为该算法的一部分 我想

随机推荐