运行程序输出传输到xargs的管道

3

我有一个C程序,通过i2c连续读取加速度计数据。每当平板电脑的方向改变时,它就会在标准输出中输出一行新内容。

现在,我想要能够在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 <(./i2c-rotation;) - David
1个回答

1
默认情况下,xargs 希望在运行命令之前读取大量的参数。在这种情况下,这可能不是您想要的。xargs -L1 在每个完整的输入行后运行命令。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接