获得呼叫者绝对路径 - Bash Mac OS X Leopard

时间:2009-09-23 15:48:14

标签: bash path jar parameters

我正在Mac OS X Leopard上的应用程序包中运行一个jar文件。我需要将一个参数传递给jar。该参数是调用app bundle的文件的绝对路径。我在下面有一个简短的bash脚本。我知道$ 0给出了应用程序包本身的绝对路径。

有谁知道如何在变量中存储我需要的路径(下面的CALLERPATH)?如果另一个脚本调用它(在Dennis的回复中找到调用者脚本),列出的当前脚本工作正常,但是当我双击“file.xyz”时,它的工作方式不同。

脚本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"

输出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

脚本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"

输出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 个答案:

答案 0 :(得分:2)

我有一台Mac,所以我不能使用 $(readlink -f $ 0)技巧。这是我的解决方案,请针对您的系统进行测试,因为我没有方便的Linux机器:

# 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

它是如何工作的?

策略是1)获取目录名称,2)cd到它,3)记录$ PWD,它始终是绝对路径格式。

答案 1 :(得分:0)

这有点像kludge,但这里有:

首先,您的代码包含“bash”,但您的shebang代表/bin/sh。我假设是Bash。

第一个来电者:

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

下一个calltest:

#!/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"

现在运行测试:

$ 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