Redirected output freezes when using tee

I would like to provide an optional logging option in a bash script and would like to use the execto teechannel from the very beginning. However, opening the process teecauses the script to freeze, I believe stdout is not closed:

# Output to a log file, if set
if [[ $OPT_LOG ]]; then
    exec > >(tee -a $OPT_LOG)
fi

I tried to close:

exec >&-

But it still freezes - is there any other way to close correctly teeso that the script exits correctly at the end of execution?

+4
source share
2 answers

It seems that for some reason, use teestops the prompt ( $PS1) because the shell script did not exit. As a workaround, I usually use short sleepafter the call tee.

#!/bin/bash

exec > >(tee -a mylog)
sleep .1
# my code
+4

.

I :

Mac OS X 10.10.3.

OPT_LOG=file.name
if [[ $OPT_LOG ]]
then exec > >(tee -a $OPT_LOG)
fi
for ((i = 0; i < 10; i++))
do
    echo "Logging message $i at $(date)"
    sleep 1
done

, , , . ? bash -x yourscript.sh?

chatraed

, script , :

OPT_LOG=file.name;
if [[ $OPT_LOG ]]; then exec > >(tee -a $OPT_LOG); fi;
for ((i = 0; i < 10; i++)); do echo "Logging message $i"; done

:

, ! .

:

  • pwd for " ", ( pwd, , Bash).
  • bash -c 'exit 0' ( ), .
  • > >(tee -a $OPT_LOG &), - . ( , , .)

, Bash, . (. Bash , .) , , , - . , Apple Bash 3.2.57; Bash 4.3.27 ( , ShellShock).

, script exec, , .

, chatraed , , . sleep 0, , sleep 0.1. ; ( POSIX) sleep . OTOH, sleep 0.1, , 0, , ; .1 .

+3

All Articles