Skip to main content
Solved

How to tell what (parent) directory a script is running from


Forum|alt.badge.img+12

Ok, maybe a little misleading for the title but I didn't know a better way to name it.

Most of the time I have casper admin open on my Mac, which includes having the volume "CasperShare" mounted on my mac. If a policy happens to run while I have CA open, it will be running from "CasperShare 1." I have several policies that run post-folght scripts that use the jamf utility to install a package, etc, and I need to specify the correct path to the package. Any quick way of telling what mounted volume a script is running from?

Best answer by mscottblake

I believe this should do the trick.

DIR=$( cd "$( dirname "$0" )" && pwd )
View original
Did this topic help you find an answer to your question?

Forum|alt.badge.img+24
  • Honored Contributor
  • December 3, 2012

I believe this should do the trick.

DIR=$( cd "$( dirname "$0" )" && pwd )

Forum|alt.badge.img+31
  • Honored Contributor
  • December 3, 2012

Yup,

Mr. Blake is on to the right solution here using a positional parameter. $0 is the full path of the script. That is why you will see things like `srm $0` at the end of a postflight script so the script removes itself after running.

Here is a proof of concept I whipped up really fast:

#!/bin/bash

# test to find full path of script

echo "$0 is the full path"

exit 0

Here is the output:

$ bash -x /Users/tlarkin/Documents/scripts/test_pwd.sh 
+ echo '/Users/tlarkin/Documents/scripts/test_pwd.sh is the full path'
/Users/tlarkin/Documents/scripts/test_pwd.sh is the full path
+ exit 0

Hope that helps you.

Thanks,
Tom


Forum|alt.badge.img+12

Thanks! Your answers set me on the right path. It's amazing what you can think of when you aren't completely losing your mind at work. It's as simple as 'cd ..' since it's a script being run from Casper, i already know it's location - CasperShare/Scripts, so if i just issue a

cd ..
Parent=`pwd`

that gives me the parent directory of the Scripts directory.... and then I just remembered that scripts don't run from the CasperShare unless you call them with the jamf binary. Is there a way to trace the running script back to the share it came from?


Forum|alt.badge.img+24
  • Valued Contributor
  • December 7, 2012

lsof is your friend. While a script is running, something like

lsof /usr/sbin/jamf

Should get you a list of open files that the jamf binary has called. That will give you the full file path and you should see a /Volumes/CasperShare or /Volumes/CasperShare 1 depending on the state of your machine (e.g. if Casper Admin is open)


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings