3

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
1
  • Try this: xargs xrandr --orientation <(./i2c-rotation;) Commented Feb 1, 2013 at 19:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.