Get Caller Absolute Path - Bash Mac OS X Leopard

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I am running a jar file from within an app bundle on Mac OS X Leopard. I need to pass into the jar a parameter. The parameter is the absolute path of the file which called the app bundle. I have my short bash script below. I know $0 gives the absolute path to the app bundle itself.

Does anyone know how to store in a variable the path I need (CALLERPATH below)? The current script listed works fine if another script calls it (find caller script in the reply from Dennis), but does not work the same 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
"
echo "Caller Path Output 2"
callerpath="${callerpath#/bin/bash *}"
echo -e "$callerpath
"
echo "Final Caller Path"
callerpath="${callerpath%/*}"
echo -e "$callerpath
"
exec java 
-jar "$BASEDIR/../Resources/Java/myJar.jar" "$callerpath"

Output 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
"
echo "Caller Path Output 2"
callerpath="${callerpath#/bin/bash *}"
echo -e "$callerpath
"
echo "Final Caller Path"
callerpath="${callerpath%/*}"
echo -e "$callerpath
"
exec java 
-jar "$BASEDIR/../Resources/Java/myJar.jar" "$callerpath"

Output 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 

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 handy:

# 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 does it work?

The strategy is 1) get the dir name, 2) cd to it, and 3) record the $PWD, which is always in absolute path format.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/1466871/get-caller-absolute-path-bash-mac-os-x-leopard

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils