假装是 bash 中的任何命令的 tty

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32910661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:40:12  来源:igfitidea点击:

Pretend to be a tty in bash for any command

bashtty

提问by edi9999

Whenever I use grep, and I pipe it to an other program, the --coloroption is not respected. I know I could use --color=always, but It also comes up with some other commands that I would like to get the exact output of that command as the output I would get if I was in a tty.

每当我使用grep, 并将其通过管道传输到其他程序时,该--color选项不受尊重。我知道我可以使用--color=always,但它也提出了一些其他命令,我想获得该命令的确切输出作为我在 tty 中获得的输出。

So my question is, is it possible to trick a command into thinking that the command is run inside a tty ?

所以我的问题是,是否有可能诱使命令认为该命令是在 tty 内运行的?

For example, running

例如,运行

grep --color word file # Outputs some colors
grep --color word file | cat # Doesn't output any colors

I'd like to be able to write something like :

我希望能够写出类似的东西:

IS_TTY=TRUE grep --color word file | cat  # Outputs some colors

This questionseems to have a tool that might do what I want :empty - run processes and applications under pseudo-terminal (PTY), but from what I could read in the docs, I'm not sure it can help for my problem

这个问题似乎有一个工具可以做我想做的事:在伪终端 (PTY) 下空运行进程和应用程序,但从我在文档中读到的内容来看,我不确定它是否可以解决我的问题

采纳答案by Nick Sweeting

There are a number of options, as outlined by several other Stack Overflow answers (see Caarlos's comment). I'll summarize them here though:

正如其他几个 Stack Overflow 答案所概述的那样,有许多选项(请参阅Caarlos评论)。不过,我会在这里总结一下:

  1. Use script+ printf, requires no extra dependencies:

    0<&- script -qefc "ls --color=auto" /dev/null | cat
    

    Or make a bash function fakettyto encapsulate it:

    faketty () {
        script -qfce "$(printf "%q " "$@")"
    }
    faketty ls --color=auto | cat  
    

    Or in the fish shell:

    function faketty
        script -qefc "(printf "%q " "$argv")"
    end
    faketty ls --color=auto | cat 
    

    (credit goes to this answer)

    http://linux.die.net/man/1/script

  2. Use the unbuffercommand(as part of the expectsuite of commands), unfortunately this requires a 50mb+ install, but it's the easiest solution:

    sudo apt-get install expect-dev
    unbuffer -p ls --color=auto | cat  
    

    Or if you use the fish shell:

    function faketty
        unbuffer -p $argv
    end
    faketty ls --color=auto | cat 
    

    http://linux.die.net/man/1/unbuffer

  1. 使用script+printf,不需要额外的依赖:

    0<&- script -qefc "ls --color=auto" /dev/null | cat
    

    或者做一个bash函数faketty来封装它:

    faketty () {
        script -qfce "$(printf "%q " "$@")"
    }
    faketty ls --color=auto | cat  
    

    或者在鱼壳中:

    function faketty
        script -qefc "(printf "%q " "$argv")"
    end
    faketty ls --color=auto | cat 
    

    (归功于此答案

    http://linux.die.net/man/1/script

  2. 使用unbuffer命令(作为expect命令套件的一部分),不幸的是这需要 50mb+ 的安装,但它是最简单的解决方案:

    sudo apt-get install expect-dev
    unbuffer -p ls --color=auto | cat  
    

    或者,如果您使用鱼壳:

    function faketty
        unbuffer -p $argv
    end
    faketty ls --color=auto | cat 
    

    http://linux.die.net/man/1/unbuffer

This is a great article on how TTYs work and what Pseudo-TTYs (PTYs) are, it's worth taking a look at if you want to understand how the linux shell works with file descriptors to pass around input, output, and signals. http://www.linusakesson.net/programming/tty/index.php

这是一篇关于 TTY 如何工作以及什么是伪 TTY (PTY) 的精彩文章,如果您想了解 linux shell 如何使用文件描述符来传递输入、输出和信号,那么值得一看。http://www.linusakesson.net/programming/tty/index.php