Skip to main content
added 265 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

Only redirecting to the $OUT file in append mode is much easier:

case $TERM_OPT in
  (OFF) exec >> "$OUT";;
  (*)   exec > >(tee -ia -- "$OUT")
esac

Beware the shell doesn't wait for tee upon exiting, so you might find that tee is still outputting something after your script has finished.

$ bash -c 'exec > >(tee -ia -- file); echo A'; echo B; sleep 1
B
A

See how A was output after B.

To work around that, you can add:

if [ "$TERM_OPT" != OFF ]; then
  exec > /dev/null # closes the pipe to tee which should cause tee
                   # to see EOF on input and exit
  wait # waits for all asynchronous tasks including tee
fi

At the end of the script or in an EXIT trap.

Another option (which would also work in sh) is to put your whole script inside a main function and do:

#! /bin/sh -
main() {
  # your script here
}
case $TERM_OPT in
  (OFF) main "$@" >> "$OUT";;
  (*)   main "$@" | tee -ia -- "$OUT"
esac

Only redirecting to the $OUT file in append mode is much easier:

case $TERM_OPT in
  (OFF) exec >> "$OUT";;
  (*)   exec > >(tee -ia -- "$OUT")
esac

Beware the shell doesn't wait for tee upon exiting, so you might find that tee is still outputting something after your script has finished.

$ bash -c 'exec > >(tee -ia -- file); echo A'; echo B; sleep 1
B
A

See how A was output after B.

To work around that, you can add:

if [ "$TERM_OPT" != OFF ]; then
  exec > /dev/null # closes the pipe to tee which should cause tee
                   # to see EOF on input and exit
  wait # waits for all asynchronous tasks including tee
fi

At the end of the script or in an EXIT trap.

Only redirecting to the $OUT file in append mode is much easier:

case $TERM_OPT in
  (OFF) exec >> "$OUT";;
  (*)   exec > >(tee -ia -- "$OUT")
esac

Beware the shell doesn't wait for tee upon exiting, so you might find that tee is still outputting something after your script has finished.

$ bash -c 'exec > >(tee -ia -- file); echo A'; echo B; sleep 1
B
A

See how A was output after B.

To work around that, you can add:

if [ "$TERM_OPT" != OFF ]; then
  exec > /dev/null # closes the pipe to tee which should cause tee
                   # to see EOF on input and exit
  wait # waits for all asynchronous tasks including tee
fi

At the end of the script or in an EXIT trap.

Another option (which would also work in sh) is to put your whole script inside a main function and do:

#! /bin/sh -
main() {
  # your script here
}
case $TERM_OPT in
  (OFF) main "$@" >> "$OUT";;
  (*)   main "$@" | tee -ia -- "$OUT"
esac
added 386 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

Only redirecting to the $OUT file in append mode is much easier:

case $TERM_OPT in
  (OFF) exec >> "$OUT";;
  (*)   exec > >(tee -ia -- "$OUT")
esac

Beware the shell doesn't wait for tee upon exiting, so you might find that tee is still outputting something after your script has finished.

$ bash -c 'exec > >(tee -ia -- file); echo A'; echo B; sleep 1
B
A

See how A was output after B.

To work around that, you can add:

if [ "$TERM_OPT" != OFF ]; then
  exec > /dev/null # closes the pipe to tee which should cause tee
                   # to see EOF on input and exit
  wait # waits for all asynchronous tasks including tee
fi

At the end of the script or in an EXIT trap.

Only redirecting to the $OUT file in append mode is much easier:

case $TERM_OPT in
  (OFF) exec >> "$OUT";;
  (*)   exec > >(tee -ia -- "$OUT")
esac

Beware the shell doesn't wait for tee upon exiting, so you might find that tee is still outputting something after your script has finished.

Only redirecting to the $OUT file in append mode is much easier:

case $TERM_OPT in
  (OFF) exec >> "$OUT";;
  (*)   exec > >(tee -ia -- "$OUT")
esac

Beware the shell doesn't wait for tee upon exiting, so you might find that tee is still outputting something after your script has finished.

$ bash -c 'exec > >(tee -ia -- file); echo A'; echo B; sleep 1
B
A

See how A was output after B.

To work around that, you can add:

if [ "$TERM_OPT" != OFF ]; then
  exec > /dev/null # closes the pipe to tee which should cause tee
                   # to see EOF on input and exit
  wait # waits for all asynchronous tasks including tee
fi

At the end of the script or in an EXIT trap.

deleted 406 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

Only redirecting to the $OUT file in append mode is much easier:

case $TERM_OPT in
  (OFF) exec >> "$OUT";;
  (*)   exec > >(tee -ia -- "$OUT")
esac

Or if switching to zsh is an optionBeware the shell doesn't wait for (your script is already more in zsh syntax than bash syntax as you're not quoting your variables):

case $TERM_OPT in
  (OFF) exec >> $OUT;;
  (*)   exec >&1 >> $OUT
esac

In zshtee upon exiting, redirecting a fd more than once for writing redirects it to a pipe to an internal process that feeds the data fromso you might find that pipe to the destinations in a tee-like fashion is still outputting something after your script has finished.

Only redirecting to the $OUT file in append mode is much easier:

case $TERM_OPT in
  (OFF) exec >> "$OUT";;
  (*)   exec > >(tee -ia -- "$OUT")
esac

Or if switching to zsh is an option (your script is already more in zsh syntax than bash syntax as you're not quoting your variables):

case $TERM_OPT in
  (OFF) exec >> $OUT;;
  (*)   exec >&1 >> $OUT
esac

In zsh, redirecting a fd more than once for writing redirects it to a pipe to an internal process that feeds the data from that pipe to the destinations in a tee-like fashion.

Only redirecting to the $OUT file in append mode is much easier:

case $TERM_OPT in
  (OFF) exec >> "$OUT";;
  (*)   exec > >(tee -ia -- "$OUT")
esac

Beware the shell doesn't wait for tee upon exiting, so you might find that tee is still outputting something after your script has finished.

Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k
Loading