Trick an application into thinking its stdout is a terminal, not a pipe

I'm trying to do the opposite of

Detect if stdin is a terminal or pipe?

I'm running an application that's changing its output format because it detects a pipe on stdout, and I want it to think that it's an interactive terminal so that I get the same output when redirecting.

I was thinking that wrapping it in an expect script or using a proc_open() in PHP would do it, but it doesn't.

Any ideas out there?


Aha!

The script command does what we want...

script --return -c "[executable string]" /dev/null

Does the trick!


Based on Chris' solution, I came up with the following little helper function:

faketty() {
    script -qfc "$(printf "%q " "$@")" /dev/null
}

The quirky looking printf is necessary to correctly expand the script's arguments in $@ while protecting possibly quoted parts of the command (see example below).

Usage:

faketty <command> <args>

Example:

$ python -c "import sys; print sys.stdout.isatty()"
True
$ python -c "import sys; print sys.stdout.isatty()" | cat
False
$ faketty python -c "import sys; print sys.stdout.isatty()" | cat
True

The unbuffer script that comes with Expect should handle this ok. If not, the application may be looking at something other than what its output is connected to, eg. what the TERM environment variable is set to.

链接地址: http://www.djcxy.com/p/25290.html

上一篇: 何时使用在Ruby中启动子进程的每种方法

下一篇: 欺骗一个应用程序,让它认为它的stdout是一个终端,而不是一个管道