Get Absolute Caller Path - Bash Mac OS X Leopard

I am running a jar file from an application bundle on Mac OS X Leopard. I need to pass a parameter to the jar. The parameter is the absolute path to a file called the application package. I have a short bash script below. I know that $ 0 gives the absolute path to the application suite itself.

Does anyone know how to store in a variable the path I need (CALLERPATH below)? The current current script works fine if another script calls it (find the calling script in Dennis's answer), but doesn't work when I double click on "file.xyz".

Script 1

#!/bin/bash
echo $0
echo $_
echo $(dirname $0)
echo $(basename $0)
echo $PWD
echo "$@"
echo $PPID
echo "My PPID echo"
myPPID=$PPID echo $(ps -p $myPPID -o args=)
BASEDIR=`dirname "$0"`
echo "CallerPath Output 1"
callerpath="$(/bin/ps -p $PPID -o args=)"
echo -e "$callerpath\n"
echo "Caller Path Output 2"
callerpath="${callerpath#/bin/bash *}"
echo -e "$callerpath\n"
echo "Final Caller Path"
callerpath="${callerpath%/*}"
echo -e "$callerpath\n"
exec java \
-jar "$BASEDIR/../Resources/Java/myJar.jar" "$callerpath"

      

Exit 1

9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496] /Applications/appBundle.app/Contents/MacOS/myScript
9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496] /Applications/appBundle.app/Contents/MacOS/myScript
9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496] /Applications/appBundle.app/Contents/MacOS 
9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496] myScript
9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496] / 
9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496] -psn_0_766139 
9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496] 63 
9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496] My PPID echo 
9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496] ps: Invalid process id: -o 
9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496] ps: illegal argument: args= 
9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496] usage: ps [-AaCcEefhjlMmrSTvwXx] [-O fmt | -o fmt] [-G gid[,gid...]] 
9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496]           [-g grp[,grp...]] [-u [uid,uid...]] 
9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496]           [-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]] 
9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496]        ps [-L] 
9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496] CallerPath Output 1 
9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496] Caller Path Output 2 
9/25/09 3:54:42 PM [0x0-0xbb0bb].MyApp[1496] Final Caller Path

      

Script 2

#!/bin/bash
echo $0
echo $_
echo $(dirname $0)
echo $(basename $0)
echo $PWD
echo "$@"
echo $PPID
echo "My PPID export"
export myPPID=$PPID
echo $(ps -p $myPPID -o args=)
BASEDIR=`dirname "$0"`
echo "CallerPath Output 1"
callerpath="$(/bin/ps -p $PPID -o args=)"
echo -e "$callerpath\n"
echo "Caller Path Output 2"
callerpath="${callerpath#/bin/bash *}"
echo -e "$callerpath\n"
echo "Final Caller Path"
callerpath="${callerpath%/*}"
echo -e "$callerpath\n"
exec java \
-jar "$BASEDIR/../Resources/Java/myJar.jar" "$callerpath"

      

Exit 2

9/25/09 4:02:40 PM [0x0-0xc10c1].MyApp[1561] /Applications/appBundle.app/Contents/MacOS/myScript
9/25/09 4:02:40 PM [0x0-0xc10c1].MyApp[1561] /Applications/appBundle.app/Contents/MacOS/myScript
9/25/09 4:02:40 PM [0x0-0xc10c1].MyApp[1561] /Applications/appBundle.app/Contents/MacOS 
9/25/09 4:02:40 PM [0x0-0xc10c1].MyApp[1561] myScript
9/25/09 4:02:40 PM [0x0-0xc10c1].MyApp[1561] / 
9/25/09 4:02:40 PM [0x0-0xc10c1].MyApp[1561] -psn_0_790721 
9/25/09 4:02:40 PM [0x0-0xc10c1].MyApp[1561] 63 
9/25/09 4:02:40 PM [0x0-0xc10c1].MyApp[1561] My PPID export 
9/25/09 4:02:40 PM [0x0-0xc10c1].MyApp[1561] CallerPath Output 1 
9/25/09 4:02:40 PM [0x0-0xc10c1].MyApp[1561] Caller Path Output 2 
9/25/09 4:02:40 PM [0x0-0xc10c1].MyApp[1561] Final Caller Path 

      

+2


source to share


2 answers


I have a Mac so I cannot use the $ (readlink -f $ 0) trick . Here is my solution, please test it against your system as I don't have a Linux machine:

# Get absolute path of the script
dir=`dirname $0`            # The directory where the script is 
pushd "$dir" > /dev/null    # Go there
CALLERPATH=$PWD             # Record the absolute path
popd > /dev/null            # Return to previous dir

echo $CALLERPATH

      



How it works?

Strategy: 1) get the name dir, 2) cd to it, and 3) write $ PWD, which is always in absolute path format.

+2


source


It's a bit shred, but here it goes:

First, your tags include "bash", but your shebang is for /bin/sh

. I'm assuming Bash.

First caller:

#!/bin/bash
/home/username/dirONE/calltest a b c

      



Next call:

#!/bin/bash
# echo some stuff to get our bearings
echo $0
echo $_                                  # should be the same as $0
echo $(dirname $0)
echo $(basename $0)
echo $PWD
echo "$@"
echo $PPID                               # parent process ID
# start making $callerpath, if there are spaces in a dirname or script name, they should be preserved
callerpath="$(ps -p $PPID -o args=)"     # get the command name of the parent (it may have args after it)
callerpath="${callerpath#/bin/bash *}"   # strip /bin/bash from the beginning
callerpath="${callerpath%/*}"            # like dirname, but removes the args, too
echo "$callerpath"

      

Now, to run the test:

$ cd /home/username/dirTHREE
$ /home/username/dirTWO/caller 1 2 3
/home/username/dirONE/calltest
/home/username/dirONE/calltest
/home/username/dirONE
calltest
/home/username/dirTHREE
a b c
32674
/home/username/dirTWO

      

0


source







All Articles