I want to run a program’s output through a pipe, but it apparently behaves differently when it detects stdout is not an interactive shell.
How can I trick it into writing through the pipe just as it would in regular circumstances?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I assume that the program will call the glibc function
isatty()to check whether stdout is a terminal or not. That’s common for programs which use colorized output on terminals or other features of an ANSI terminal like cursor positioning or line erasing / redrawing.You can trick the program using the LD_PRELOAD environment variable. LD_PRELOAD is handled by the ELF linker and tells that a dynamic library should be loaded before all others. Using this feature it is possible to override library functions, in your case the glibc function
isatty(). You can follow this article for example.I’ve prepared an example for you:
First create the file libisatty.c:
and compile it as a shared lib:
It should build fine.
Now it’s time to test the library. 🙂 I’ve used the command
ls --color=autofor tests.lscallsisatty()to decide whether it should colorize it’s output or not. If the output is redirected to a file or a pipe it won’t be colorized. You can test this easily using the following commands:Now we’ll try the second command again using the LD_PRELOAD environment var:
You should see colorized output.
btw cool usename: uʍop ǝpısdn !!:D