bash - Process substitution return prompt -
i'm writing bash script , using following trick redirect standard output named pipe consumed tee:
exec > >(tee -a $logfile) 2>&1 however, when script exits, not return shell until press enter. there simple way fix while still using approach?
edit: environment i'm running in:
centos 7 bash version 4.2.45 contents of simple script called redirect.sh:
#!/bin/bash exec > >(tee -a /tmp/haha) 2>&1 echo "hi there" exit 0 sample session:
[root@linux-ha-1 ~]# ./redirect.sh [root@linux-ha-1 ~]# hi there [root@linux-ha-1 ~]#
the prompt being printed; unfortunately, printed before tee's output printed (which why appears before hi there in sample output).
since tee process running asynchronously, there no guarantee send output console before script terminates. want close tee process , wait terminate before exiting script. cannot done process substitution, unfortunately, can accomplished either coprocesses (in bash 4) or using named pipes, explained in answer bash: how ensure termination of process substitution used exec?
for simpler (but unreliable) solution, close pipes feeding tee process (which force close) , wait few milliseconds:
#!/bin/bash exec 3>&1 > >(tee -a /tmp/haha) 2>&1 echo "hi there" exec 1>&3 2>&3 sleep 0.1
Comments
Post a Comment