获取由符号链接调用的 KornShell 脚本的“dirname $0”
我的文件夹组织如下所示:
link.sh
dist/MyApp-3.0.0/script.sh
dist/MyApp-3.0.0/lib/*.jar
link.sh
是指向 KornShell (ksh) 脚本 script.sh
的符号链接。在 shell 脚本中,我想使用以下命令调用 Java 程序:
java -cp lib/*
当我尝试从符号链接启动应用程序时,我得到 ClassNotFound
因为相对路径是从链接基目录解析的(这是正常的)。
在 shell 脚本中,如何获取脚本的完整路径 (<...>/dist/MyApp-3.0.0/
)?它将允许我修改我的 Java 调用:
java -cp ${SCRIPT_DIR}/lib/*
I have a folder organization that looks like this:
link.sh
dist/MyApp-3.0.0/script.sh
dist/MyApp-3.0.0/lib/*.jar
The link.sh
is a symbolic link to the KornShell (ksh) script script.sh
. In the shell script, I want to call a Java program with following command:
java -cp lib/*
When I try to launch the application from the symbolic link, I get ClassNotFound
because the relative path is resolved from the link base dir (this is normal).
Inside the shell script, how can I get the full path of the script (<...>/dist/MyApp-3.0.0/
)? It will allow me to modify my Java call:
java -cp ${SCRIPT_DIR}/lib/*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:使用readlink
您可以使用readlink,它归结为:
编辑:不使用readlink
我误解了脚本的位置,并假设脚本的目录被调用的是目标应用程序的目录结构,其中以下内容可以工作:
Edit: using readlink
You can use readlink, and it boils down to:
Edit: without readlink
I misunderstood the location of the script, and had assumed that the directory of the script being invoked was in the directory structure of the target application, where the following would work:
你必须使用 readlink 函数 (man readlink)
my2c
You have to use the readlink function (man readlink)
my2c