伪装成bash中的tty来执行任何命令 [英] Pretend to be a tty in bash for any command

查看:178
本文介绍了伪装成bash中的tty来执行任何命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我使用grep并将其通过管道传输到其他程序时,就不会使用--color选项.我知道我可以使用--color=always,但是它还提供了一些其他命令,我希望获得该命令的确切输出,就像我在tty中一样.

所以我的问题是,是否有可能欺骗某个命令以使其认为该命令在tty中运行?

例如,运行

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

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

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

空-在以下环境下运行进程和应用程序伪终端(PTY),但是从我在文档中可以看到的内容来看,我不确定它是否可以解决我的问题

解决方案

还有其他一些Stack Overflow答案所概述的选项(请参阅评论).我会在这里总结一下:

  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

这是一篇很棒的文章,介绍了TTY的工作方式以及什么是Pseudo-TTY(PTY),如果您想了解linux shell如何与文件描述符一起传递输入,输出和信号,那么值得一看. . http://www.linusakesson.net/programming/tty/index.php

Whenever I use grep, and I pipe it to an other program, the --color option 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.

So my question is, is it possible to trick a command into thinking that the command is run inside a 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 question seems 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

解决方案

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

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

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

    Or make a bash function faketty to 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 unbuffer command (as part of the expect suite 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

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

这篇关于伪装成bash中的tty来执行任何命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆