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

acdesigntech
Contributor II

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?

1 ACCEPTED SOLUTION

mscottblake
Valued Contributor

I believe this should do the trick.

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

View solution in original post

4 REPLIES 4

mscottblake
Valued Contributor

I believe this should do the trick.

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

tlarkin
Honored Contributor

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

acdesigntech
Contributor II

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?

jarednichols
Honored Contributor

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)