File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
scripts/bash_io_redirection Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 1+ #! /bin/bash
2+
3+ # Open the log file we want to write to as FD 7
4+ exec {LOGFD}> /tmp/test.log
5+
6+ # Open a new FD 8 that copies its input to FDs 7 and 1 (log file and stdout).
7+ # By using the FD instead of directly writing to the file, we help tee cope
8+ # with file position issues. Hopefully.
9+ exec {DUPFD}> >( tee /dev/fd/${LOGFD} )
10+
11+ # Send echo to fd1 (stdout). Result will only be on stdout.
12+ echo ' testing fd1'
13+
14+ # Send echo to FD8. Result is going to be on on the log and on stdout. Hopefully.
15+ echo ' testing fd7' >& ${DUPFD}
16+
17+ # YESSSS
18+
19+ # Now what about the other way? We want to copy stdout and stderr to a log file
20+ # along with script trace info, but also emit stdout to the terminal.
21+ #
22+ # So we need to open a log file:
23+ exec {LOGFD2}> /tmp/test2.log
24+
25+ # copy stdout to the logfile via tee, retaining stdout too
26+ exec {STDOUTCOPY}> >( tee /dev/fd/${LOGFD2} )
27+
You can’t perform that action at this time.
0 commit comments