Residuum
Residuum

Reputation: 12064

Piping Output from a Running Program into xargs

I have a C program that reads accelerometer data continously via i2c. Whenever the orientation of the tablet has changed, it outputs a new line to stdout.

Now, I want to be able to use that output in a bash script to change the rotation of the screen.

Now, the problem is this: When I view the output of the program in bash, the program is outputting the changes line by line. 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:

#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:

#!/bin/bash

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

Upvotes: 3

Views: 315

Answers (1)

Anton Kovalenko
Anton Kovalenko

Reputation: 21507

By default, xargs wants to read a lot of arguments before running a command with them all. It's probably not what you want in this case.

xargs -L1 runs the command after each complete line of input.

Upvotes: 1

Related Questions