Restoring stdout and stderr to default value

后端 未结 2 460
名媛妹妹
名媛妹妹 2021-01-12 00:19

In shell script, We can change the default input to a File using the exec command as follow :

  exec 1>outputfile

However, if in the sa

相关标签:
2条回答
  • 2021-01-12 00:45

    Try the "tee" command:

    exec | tee outputfile
    

    Look at the tee manpage for more explainations:

    tee - read from standard input and write to standard output and files

    0 讨论(0)
  • 2021-01-12 01:03

    This example

    Example 20-2. Redirecting stdout using exec

    #!/bin/bash
    # reassign-stdout.sh
    
    LOGFILE=logfile.txt
    
    exec 6>&1           # Link file descriptor #6 with stdout.
                        # Saves stdout.
    
    exec > $LOGFILE     # stdout replaced with file "logfile.txt".
    
    # ----------------------------------------------------------- #
    # All output from commands in this block sent to file $LOGFILE.
    
    echo -n "Logfile: "
    date
    echo "-------------------------------------"
    echo
    
    echo "Output of \"ls -al\" command"
    echo
    ls -al
    echo; echo
    echo "Output of \"df\" command"
    echo
    df
    
    # ----------------------------------------------------------- #
    
    exec 1>&6 6>&-      # Restore stdout and close file descriptor #6.
    
    echo
    echo "== stdout now restored to default == "
    echo
    ls -al
    echo
    
    exit 0
    

    appears to show what you want. It came from the ABS, where there is a small amount of discussion and other relevant information.

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