Sanchit Khera Sanchit Khera - 1 month ago 19
Linux Question

Restrict logging for a particular child shell script

I'm running a bunch of scripts through one main parent script. I have added logging code in the parent script like below:

exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>logFileName 2>&1


This generates logs for all child scripts. I want to skip the log generation for one particular script, since its logs can be very heavy at times.

Is there any way to do this? Many thanks!

My parent script looks like this:

mkdir -p $scriptFolder/log
logFile="$scriptFolder/log/$(echo "$4" | tr -d '\n\r')_log_ImportMain_$dateTime.log"
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>$logFile 2>&1
source $scriptFolder/shellscripts/1.copyFilesFromFTP.sh
source $scriptFolder/shellscripts/2.import.sh
source $scriptFolder/shellscripts/3.export.sh
source $scriptFolder/shellscripts/4.copyFilesToFTP.sh


I want to skip logging for 3.export.sh, is there any way possible to do this?

Answer

You are not running the scripts as subprocesses, which your question implies with the "parent" term, but anyway, you can have another redirection for that individual command, like this:

mkdir -p "$scriptFolder"log       # notice quoting
logFile="$scriptFolder/log/$(echo "$4" | tr -d '\n\r')_log_ImportMain_$dateTime.log"
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3 15  # notice addition of 15
exec 1>"$logFile" 2>&1            # notice quoting here, too
source $scriptFolder/shellscripts/1.copyFilesFromFTP.sh
source $scriptFolder/shellscripts/2.import.sh
# Temporarily disable logging for this source statement
source $scriptFolder/shellscripts/3.export.sh >/dev/null 2>&1
source $scriptFolder/shellscripts/4.copyFilesToFTP.sh

The >/dev/null 2>&1 is in effect for the duration of the third source statement, and file descriptors are restored to their previous state when that finishes.

By the way, are you really sure "$4" can contain embedded newlines, and that you want to remove them in that case? The trailing newline from echo will be trimmed off anyway, so perhaps you simply want logfile="$scriptfolder/log/${4}_log_ImportMain_$dateTime.log"

Comments