Created
September 9, 2019 14:38
-
-
Save fishd72/34c9b12c99359bbcb2ba69ec13fa2e98 to your computer and use it in GitHub Desktop.
Bash logging function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to write to the Log file | |
# Credit to http://linuxadvices.blogspot.com/2013/05/logging-in-bash-nice-subroutine-to-reuse.html | |
################################### | |
write_log() | |
{ | |
while read text | |
do | |
LOGTIME=`date "+%Y-%m-%d %H:%M:%S"` | |
# If log file is not defined, just echo the output | |
if [ "$LOG_FILE" == "" ]; then | |
echo $LOGTIME": $text"; | |
else | |
LOG=$LOG_FILE.`date +%Y%m%d` | |
touch $LOG | |
if [ ! -f $LOG ]; then echo "ERROR!! Cannot create log file $LOG. Exiting."; exit 1; fi | |
echo $LOGTIME": $text" | tee -a $LOG; | |
fi | |
done | |
} | |
It is easy to log any output to such subroutine, just pipe the output as folowing: | |
echo "Skipping to next copy" | write_log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment