I'm looking for a solution in ksh93 that will let me run a command and capture the STDERR and STDIN streams to a log file. I also want STDIN and STDERR to output to the terminal. Optionally, in some cases, I would like to capture STDIN to a log file as well (but separate from STDERR).
I have a partial solution, derived from Copy stderr and stdout to a file as well as the screen in ksh. I added the code for the 3rd named pipe, which is where I'm having the problem.
#!/bin/ksh
pipe1="/tmp/mypipe1.$$"
pipe2="/tmp/mypipe2.$$"
pipe3="/tmp/mypipe3.$$"
trap 'rm "$pipe1" "$pipe2"' EXIT
mkfifo "$pipe1"
mkfifo "$pipe2"
mkfifo "$pipe3"
tee -a out1.log < "$pipe1" &
tee -a out2.log >&2 < "$pipe2" &
tee -a out2.log < "$pipe3" &
# Redirect all script output to a logfile as well as their normal locations
exec >"$pipe1"
exec 2>"$pipe2"
exec "$pipe3"<0 # 0: cannot open [No such file or directory]
unzip $1
The above code gives the error 0: cannot open [No such file or directory]
on line 20 (see comment in code).
If I try changing the order of the redirection on line 20 with exec 0>"$pipe3
, the script seems to operate normally, reflecting all 3 I/O streams to the terminal
replace ATO-9500.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: ATO-9500.csv
replace ATO-9501.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: ATO-9501.csv
replace ATO-9502.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
inflating: ATO-9502.csv
inflating: CAPACITOR-9500.csv
inflating: CAPACITOR-9501.csv
inflating: CAPACITOR-9502.csv
but the output log (out2.log) doesn't contain the user input, and it also merges the STDERR output into a single line:
replace ATO-9500.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename: replace ATO-9501.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename: replace ATO-9502.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename:
=============
Just to mention, I've tried the process substitution code here for capturing STDERR to a log file, and my ksh man pages say that it supports process substitution, but I haven't been able to get it to work for me, so it looks like I'm stuck using the longer approach with named pipes and such.