Skip to content

Commit 7516769

Browse files
author
Craig Ringer
committed
Experiments in bash I/O redirection
1 parent c7839e3 commit 7516769

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+

0 commit comments

Comments
 (0)