简体   繁体   English

将正在运行的程序的输出管道输入xargs

[英]Piping Output from a Running Program into xargs

I have a C program that reads accelerometer data continously via i2c. 我有一个C程序,可以通过i2c连续读取加速度计数据。 Whenever the orientation of the tablet has changed, it outputs a new line to stdout. 每当平板电脑的方向发生变化时,它都会向stdout输出一个新行。

Now, I want to be able to use that output in a bash script to change the rotation of the screen. 现在,我希望能够在bash脚本中使用该输出来更改屏幕的旋转。

Now, the problem is this: When I view the output of the program in bash, the program is outputting the changes line by line. 现在,问题是:当我在bash中查看程序的输出时,程序逐行输出更改。 When I redirect the output to a file, output is written continously in the file, but when I try to pipe the output, nothing is happening. 当我将输出重定向到文件时,输出会连续写入文件中,但是当我尝试管道输出时,没有任何事情发生。

Here is the C program: 这是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);
}

And here is my trial in bash: 这是我在bash的审判:

#!/bin/bash

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

By default, xargs wants to read a lot of arguments before running a command with them all. 默认情况下, xargs想要在运行命令之前读取大量参数。 It's probably not what you want in this case. 在这种情况下,它可能不是你想要的。

xargs -L1 runs the command after each complete line of input. xargs -L1在每个完整的输入行之后运行命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM