Skip to main content
Question

parameter for "name of this script" ?

  • April 14, 2016
  • 2 replies
  • 18 views

Forum|alt.badge.img+5

I have a policy with a bunch of maintenance & system tweak scripts that sometimes takes forever to run. In trying to troubleshoot it, I thought I'd add a couple of lines to the end of each script for additional logging. Does anyone know if / how I can create a parameter that will return the name of the script?
That would allow me to do something like:

MYDATE=$(date '+%a %b %d %H:%M:%S');
echo $MYDATE $2 "Finished running “ " $[magic-parameter-for-script-name] >> /var/log/jamf.log;

is this doable?

2 replies

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • April 14, 2016

I think something like this should work, though I did not test this when running from a Casper Suite policy. Can't see why it wouldn't actually work in that case as well though.

#!/bin/sh

nameOfScript="$(basename "$0")"

echo "The script that is running is called: $nameOfScript"

Example. I saved the script to my Desktop, named "my great script.sh" (yes, with the spaces)

The output I get from running it in Terminal is:

The script that is running is called: my great script.sh

Forum|alt.badge.img+5
  • Author
  • Contributor
  • May 4, 2016

thanks, I'll try that!