How to trick an application into thinking its stdout is a terminal, not a pipe

前端 未结 9 1477
梦如初夏
梦如初夏 2020-11-22 11:58

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 pip

相关标签:
9条回答
  • 2020-11-22 12:35

    There's also a pty program included in the sample code of the book "Advanced Programming in the UNIX Environment, Second Edition"!

    Here's how to compile pty on Mac OS X:

    man 4 pty  #  pty -- pseudo terminal driver
    
    open http://en.wikipedia.org/wiki/Pseudo_terminal
    
    # Advanced Programming in the UNIX Environment, Second Edition
    open http://www.apuebook.com
    
    cd ~/Desktop
    
    curl -L -O http://www.apuebook.com/src.tar.gz
    
    tar -xzf src.tar.gz
    
    cd apue.2e
    
    wkdir="${HOME}/Desktop/apue.2e"
    
    sed -E -i "" "s|^WKDIR=.*|WKDIR=${wkdir}|" ~/Desktop/apue.2e/Make.defines.macos
    
    echo '#undef _POSIX_C_SOURCE' >> ~/Desktop/apue.2e/include/apue.h
    
    str='#include   <sys/select.h>'
    printf '%s\n' H 1i "$str" . wq | ed -s calld/loop.c
    
    str='
    #undef _POSIX_C_SOURCE
    #include <sys/types.h>
    '
    printf '%s\n' H 1i "$str" . wq | ed -s file/devrdev.c
    
    str='
    #include <sys/signal.h>
    #include <sys/ioctl.h>
    '
    printf '%s\n' H 1i "$str" . wq | ed -s termios/winch.c
    
    make
    
    ~/Desktop/apue.2e/pty/pty ls -ld *
    
    0 讨论(0)
  • 2020-11-22 12:36

    Too new to comment on the specific answer, but I thought I'd followup on the faketty function posted by ingomueller-net above since it recently helped me out.

    I found that this was creating a typescript file that I didn't want/need so I added /dev/null as the script target file:

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

    0 讨论(0)
  • 2020-11-22 12:42

    I was trying to get colors when running shellcheck <file> | less, so I tried the above answers, but they produce this bizarre effect where text is horizontally offset from where it should be:

    In ./all/update.sh line 6:
                              for repo in $(cat repos); do
                                                                      ^-- SC2013: To read lines rather than words, pipe/redirect to a 'while read' loop.
    

    (For those unfamiliar with shellcheck, the line with the warning is supposed to line up with the where the problem is.)

    In order to the answers above to work with shellcheck, I tried one of the options from the comments:

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

    This works. I also added --return and used long options, to make this command a little less inscrutable:

    faketty() {                       
        0</dev/null script --quiet --flush --return --command "$(printf "%q " "$@")" /dev/null
    }
    

    Works in Bash and Zsh.

    0 讨论(0)
提交回复
热议问题