Last active
December 5, 2024 22:39
-
-
Save mckartha/7e0241c29cc018647954b20de68c35b9 to your computer and use it in GitHub Desktop.
logexec.sh runs a command with params and logs stderr & stdout output to a timestamped logfile
This file contains hidden or 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
# [2016Dec26 MCK] - logexec.sh - this script runs the given command, redirects Stderr to Stdout, & tees the output to the screen and a logfile | |
# [2021Feb22 MCK] - updated to re-write the leading '.' character when scripts from the current directory are executed | |
# [2022Nov03 MCK] - commented out conditional around shortname test on line 27/28 since the '[[' causes errors | |
# [2024Dec05 MCK] - Adding log heading to fully print CMD before running it | |
TODAY=`date "+%Y%m%b%d-%H%M"` | |
usage () | |
{ | |
echo " | |
# | |
# Usage: use $0 to execute a command (ie: apt-get install) and log the output to a logfile with today's date | |
# | |
# this script takes one (1) or more parameters: | |
# * the command to execute, and any other parameters on the same command-line | |
# | |
" | |
} | |
if [ $# -lt 1 ] | |
then usage | |
else | |
CMD="$*" | |
LOGNAME=$(echo $CMD| sed -e 's/^\./_/ ; s/[^A-Za-z0-9._-]/_/g') | |
MAXLENGTH=80 | |
lenLOGNAME=${#LOGNAME} | |
# this if/then conditional fails due to the '[[' being interpreted as command that cannot be found | |
#if [[ "$lenLOGNAME" -gt "$MAXLENGTH" ]] | |
#then | |
SHORTNAME=${LOGNAME:0:$MAXLENGTH} | |
LOGNAME=$SHORTNAME | |
#fi | |
echo -e "======= \n >>> $CMD \n=+=+=+= \n" 2>&1 | tee -a ${LOGNAME}.$TODAY.log | |
( $CMD ) 2>&1 | tee -a ${LOGNAME}.$TODAY.log | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment