【问题标题】:Piping Output from a Running Program into xargs将正在运行的程序的输出通过管道传输到 xargs
【发布时间】:2013-02-01 19:23:43
【问题描述】:

我有一个通过 i2c 连续读取加速度计数据的 C 程序。每当数位板的方向发生变化时,它都会向标准输出输出一个新行。

现在,我希望能够在 bash 脚本中使用该输出来更改屏幕的旋转。

现在,问题是这样的:当我在 bash 中查看程序的输出时,程序正在逐行输出更改。当我将输出重定向到文件时,输出会连续写入文件中,但是当我尝试管道输出时,什么都没有发生。

这是 C 程序:

#include <stdio.h>

int main(int argc, char *argv[])
{
    int changed;
    char *orientation;

    while (1) {
        /* Read data from i2c, check for change in orientation */
        if (changed) {
            fprintf(stdout, "%s\n", orientation);
            fflush(stdout);
            changed = 0;
        }
    }

    exit(0);
}

这是我在 bash 中的试用:

#!/bin/bash

# This does not work, xrandr is not called.
./i2c-rotation | xargs xrandr --orientation 
# This is working
#./i2c-rotation > output

【问题讨论】:

  • 试试这个:xargs xrandr --orientation &lt;(./i2c-rotation;)

标签: c bash shell


【解决方案1】:

默认情况下,xargs 想要在运行命令之前读取大量参数。在这种情况下,这可能不是您想要的。

xargs -L1 在每个完整的输入行之后运行命令。

【讨论】:

    猜你喜欢
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多