Verbose Scripts

jogle
New Contributor

So I'm building a script that does our base configuration. It's executed as a policy through a custom event trigger in terminal and it would be great if I could add in something a little more verbose to actually see what's progressing. I tried using echo but it doesn't seem like it likes to execute that command.

3 REPLIES 3

tlarkin
Honored Contributor

I have a logger I have built for my DEP Notify workflow and I have used it in other areas. I have an example in my DEP Notify script you can look at here and see if this may also fit your needs.

You can also always use set -x in your bash scripts but be mindful every line of output from stdout is a row in your database, and too many of those makes baby Crom cry. It also doesn't scale.

If you are manually doing this the jamf binary has a global flag for -verbose

jhalvorson
Valued Contributor

Near the top of all my scripts is this handy section from @rtrouton

##########################################################################################
# Create time date stamped log to record actions taken by this script.
##########################################################################################
logFile="/private/var/log/COMPANYNAMETech.log"

log () {
    /bin/echo $1
    /bin/echo $(date "+%Y-%m-%d %H:%M:%S: ") $1 >> $logFile 
}

log "-----"
log "Beginning our important script"


log “the stuff will happen”
# stuff here
log “the stuff happened”

log “Completed our important script"

You can set the path and log name to what you like in the logFile variable. For each step in the script, you can add more log "say something about the step".
It's slick!!!

mcmulligan
New Contributor II

I was just updating my script template and I ran across this thread.... I have a similar practice. Any thoughts?

[Top of script, after the description and other header info]
# Variables
# Today
today=$(date +%m-%d-%Y)
scriptMission="Software-Install-For-Example"       # no spaces please
logDir="/path/to/logs"
LOG="$logDir/$scriptMission-$today.log"
# Set up log file, folder and function
if [ ! -d "$logDir" ];
then
    mkdir -p $logDir
fi
touch "$LOG"
logme()
{
    # Check to see if function has been called correctly
    if [ -z "$1" ]
    then
        echo $( date )" - logme function call error: no text passed to function! Please recheck code!"
        exit 1
    fi
    # Log the passed details
    echo "" >> $LOG
    echo $( date )" - "$1 >> $LOG
    echo "" >> $LOG
}
# Script starts
logme "This script is useful because..."

## Stuff Happens

# Script Ends
logme "Done - $today"
exit;