-1

command to redirect output to console and to a file at the same time works fine in bash. But how do i make it work in korn shell(ksh).

All my scripts runs on korn shell so cant change them to bash for this particular command to work.

exec > >(tee -a $LOGFILE) 2>&1

  • Can you use `script`? – Walter A Jun 02 '19 at 21:54
  • Hi Walter, Sorry could you please able to elaborate. – Aravind raj Jun 03 '19 at 12:22
  • `script` is a utility. `man script` shows a way to record everything that is written to the console. I think your version of ksh doesn't support `>(..)`, so you should think what you want and can do. Perhaps you can `./my_ksh_script 2>&1 | tee -a ${LOGFILE}` but I understand you want to have this inside your ksh-script. – Walter A Jun 03 '19 at 15:55

2 Answers2

0

In the code beneath I use the variable logfile, lowercase is better.
You can try something like

touch "${logfile}"
tail -f "${logfile}"&
tailpid=$!
trap 'kill -9 ${tailpid}' EXIT INT TERM
exec 1>"${logfile}" 2>&1
Walter A
  • 19,067
  • 2
  • 23
  • 43
0

A not too unreasonable technique is to re-exec the shell with output to tee. That is, at the top of the script, do something like:

#!/bin/sh

test -z "$REXEC" && { REXEC=1 exec "$0" "$@" | tee -a $LOGFILE; exit; }
William Pursell
  • 204,365
  • 48
  • 270
  • 300