0

I have the following code. I am attempting to capture both output and errors to log file and console. When the code is ran, it shows the usage but then pauses for a carriage return. Not a big deal but just wondering how this need for a carriage return can be eliminated? I have narrowed it down to the exec line as noted below. How can I achieve this without the need for the carriage return? Thank you!

#!/bin/bash

log_file=$0.log

# The following line causes need for carriage return
exec > >(tee -a ${log_file} )
exec 2> >(tee -a ${log_file} >&2)

usage()
{
  echo
  echo "usage: $0 --option1 --option2 --option3 --option4 | [-h]"
  echo
}

while [ "$1" != "" ]; do
  case $1 in
    --option1 )            shift
                           OPTION1=$1
                           ;;
    --option2 )            shift
                           OPTION2=$1
                           ;;
    --option3 )            shift
                           OPTION3=$1
                           ;;
    --option4 )            shift
                           OPTION4=$1
                           ;;
    -h | --help )          usage
                           exit
                           ;;
    * )                    usage
                           exit 1
esac
shift
done

usage
exit

1 Answer 1

1

There is no pause caused by any commands in the script. The script runs successfully until completion. The aparent pause is seen because the shell prompt gets mixed with the script output and the normal prompt waiting point is located after the last logged line emitted by tee. This is more evident running the script with bash -x (the time/date/dir data appears because I configured the prompt to show it):

15:28:38 - mié mar 02 - Dir: ~
user@host$ bash -x testexec.sh
+ log_file=testexec.sh.log
+ exec
+ exec
++ tee -a testexec.sh.log
++ tee -a testexec.sh.log
15:28:38 - mié mar 02 - Dir: ~
user@host$
usage: /home/manto/testexec.sh --option1 --option2 --option3 --option4 | [-h]

+ '[' '' '!=' '' ']'
+ usage
+ echo
+ echo 'usage: /home/manto/testexec.sh --option1 --option2 --option3 --option4 | [-h]'
+ echo
+ exit

The script ended normally and the cursor appears just under the last +exit, where the system is waiting for the next command. You can confirm this by typing pwd and pressing ENTER and the shell will show the current directory. If, instead, you only press ENTER, it is shown the shell prompt again, as expected.

Answering the question, you could show the prompt by including a single sleep 1 command just before each exit command, so the system get some time to flush the output.

This is caused because the logging redirections are configured with process substitutions [ >() and >>() ], and the tee command runs concurrently with the script, so when it ends, tee is still sending text to the terminal and the log file.

1
  • The sleep command did the trick, thank you! Commented Mar 3, 2022 at 21:42

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.