我希望脚本能在任意目录执行,这就需要获得脚本的路径,然后确定jar包所在的位置,加上绝对路径。后来看了Ant跟Tomcat的启动脚本(写得很长,很复杂)。获得脚本执行路径只需要很少的代码,命令如下:
Windows:
%~dp0
Unix/Linux/MacOSX:
$(cd -P -- "$(dirname -- "$0")" && pwd -P)
1 参考资料
* http://svn.apache.org/repos/asf/lenya/branches/trunk-rework-pubconf/tools/bin/ant.batrem %~dp0 is expanded pathname of the current script under NT
set DEFAULT_ANT_HOME=%~dp0..
* http://svn.apache.org/repos/asf/lenya/branches/trunk-rework-pubconf/tools/bin/ant
* Re: How to get the dirname of a bash shell script inside the script? - Author: Database Administrator - dbaspot.com
* Which directory is that bash script in? - Author: chris - www.electrictoolbox.com
No comments:
Post a Comment