Write to terminal after redirecting stdout to a file without using stderr?

前端 未结 2 1382
别跟我提以往
别跟我提以往 2020-12-16 23:14

I have two shell scripts, one that serves as the main \"program\" and another that serves as a \"library.\"

In several places in the \"program,\" I\'ll do something

相关标签:
2条回答
  • 2020-12-16 23:50

    Open /dev/tty on another FD.

    exec 0< /dev/null
    exec 1> /dev/null
    exec 2> /dev/null
    exec 3> /dev/tty
    echo 'Hello, World!' >&3 
    
    0 讨论(0)
  • 2020-12-17 00:06

    You can write directly to /dev/tty each time you want to write to the terminal:

    echo "hello world" > /dev/tty
    

    For a small example:

    $ cat writer.sh 
    #!/bin/sh
    
    echo "standard output"
    echo "standard error" >&2
    
    echo "direct to terminal" > /dev/tty
    $ ./writer.sh > /tmp/out 2> /tmp/err
    direct to terminal
    $ cat /tmp/out
    standard output
    $ cat /tmp/err
    standard error
    $ 
    
    0 讨论(0)
提交回复
热议问题