title: Get Caller Absolute Path - Bash Mac OS X Leopard
body: <p>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. </p>
  
  <p>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".</p>
  
  <p>Script 1</p>
  
  <pre><code>#!/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"
  </code></pre>
  
  <p>Output 1</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Script 2</p>
  
  <pre><code>#!/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"
  </code></pre>
  
  <p>Output 2</p>
  
  <pre><code>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 
  </code></pre>