I want to create a bash script which does following:
- write stdout and stderr to terminal
- write stdout and stderr to info.log and prefix it with date and time
- write stderr to error.log and prefix it with date and time
- call multiple other bash scripts and exit if one does not exit with 0
- exit with same exit code from first failing script
In my current script, the output in the terminal is not in chronological order... seems that the redirection with tee
does something asynchronous.
Here is my current script:
#!/bin/bash
info_log_path="info.log"
error_log_path="error.log"
bashtrap()
{
echo "Job finished with Exitcode $?"; exit $?;
}
trap bashtrap EXIT ERR
#exit on first errror:
#set -e
# redirect stdout and stderr to info log
exec &> >( tee >(while IFS= read -r line; do printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$line"; done >>"$info_log_path"))
# redirect stderr to error log
exec 2> >( tee >(while IFS= read -r line; do printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$line"; done >>"$error_log_path"))
echo "Executing script1"
./script1
echo "Executing script2"
./script2
echo "Executing script3"
./script3
echo "Executing script4"
./script4