9

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?

salezica
  • 74,081
  • 25
  • 105
  • 166

3 Answers3

18

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:

/**
 * Overrides the glibc function. Will always return true.
 *
 * Note: Although this should be ok for most applications it can
 * lead to unwanted side effects. It depends on the question
 * why the programm calls isatty()
 */
int isatty(int param) {
    return 1;
}

and compile it as a shared lib:

gcc -shared -o libisatty.so  libisatty.c

It should build fine.

Now it's time to test the library. :) I've used the command ls --color=auto for tests. ls calls isatty() 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:

ls --color=auto        # should give you colorized output
ls --color=auto | cat  # will give you monochrome output

Now we'll try the second command again using the LD_PRELOAD environment var:

LD_PRELOAD=./libisatty.so ls --color=auto | cat

You should see colorized output.

btw cool usename: uʍop ǝpısdn !!:D

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Warning: creating a shell function that does this `LD_PRELOAD` trick does not quite work, since apparently other things are other observable differences in the shell function (I'm not sure what they are). However, creating an alias `trick_tty` with `alias trick_tty="LD_PRELOAD=/libisatty.so "` and then doing `trick_tty | less` works. – ntc2 Mar 02 '16 at 20:36
  • 1
    For MacOS users, you can do the same thing but instead of setting LD_PRELOAD, you set DYLD_INSERT_LIBRARIES and DYLD_FORCE_FLAT_NAMESPACE, like this: `DYLD_INSERT_LIBRARIES=./libisatty.so DYLD_FORCE_FLAT_NAMESPACE=y ls -G | cat` (note the --color flag doesn't work on mac's ls) – Christopher Shroba Mar 13 '17 at 14:43
2

Using script works for me:

script outputfile.txt yourcommand
# any output ends up in outputfile.txt

You could use this to pipe I guess:

script out.txt yourcommand ; cat out.txt | nextcommand
dsummersl
  • 6,588
  • 50
  • 65
  • Examining the source code of `script` should reveal what other things, besides `isatty`, are used by programs to tell if they are running interactively. – ntc2 Mar 02 '16 at 20:37
0

You may want to try this:

./script.sh < `tty` > output

If the program is doing something like isatty(0), this may be enough.

Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • 3
    Note that the programm in question will run isatty() on stdout. You pipe the terminal to stdin. This will not solve the problem – hek2mgl Feb 04 '13 at 15:45
  • It may actually be testing stdin and not stdout – Arnaud Le Blanc Feb 04 '13 at 15:47
  • Most programs in coreutils or utils-linux are doing this. Examples: rm, mv, ls, nohup, cfdisk, more, pg, ... – Arnaud Le Blanc Feb 04 '13 at 16:01
  • please try `ls --color=auto < \`tty\` | cat` Do you see colors? Me not. (I assume that you have evaled dircolors) – hek2mgl Feb 04 '13 at 16:03
  • Note that I have to leave for a while. Will have a look later here. Think the question is interesting. Although I believe that if the programm calls isatty on stdout it isn't easy. see you! :) – hek2mgl Feb 04 '13 at 16:07
  • I'm not in my workstation now, but will give this a try later. _A priori_, i'm inclined to think like @hek2mgI, but who knows – salezica Feb 04 '13 at 18:14