bash storing the output of set -x to log file

后端 未结 1 914
别那么骄傲
别那么骄傲 2021-01-04 21:29

I have a simple download script and I use set -x which works great; I can see each step it performs, and I can identify errors in the script or in the download:

相关标签:
1条回答
  • 2021-01-04 22:02

    Assuming bash 4, BASH_XTRACEFD can be set to override the file descriptor (by default 2, stderr) to which set -x output is written:

    short_date=$(/bin/date +%m%d%y)
    exec {BASH_XTRACEFD}>>"$short_date".log
    set -x
    

    If running bash 4.0 rather than 4.1 or newer, you have BASH_XTRACEFD but not automatic file descriptor allocation, meaning you'll need to assign one yourself; in the below example, I'm picking file descriptor 100:

    short_date=$(/bin/date +%m%d%y)
    exec 100>>"$short_date".log
    BASH_XTRACEFD=100
    set -x
    

    For older releases, your only choice is to redirect all of stderr, rather than only the xtrace stream:

    short_date=$(/bin/date +%m%d%y)
    exec 2>>"$short_date.log"
    set -x
    
    0 讨论(0)
提交回复
热议问题