脚本名:a.sh
位置:/tmp/whuang/study/Java
脚本内容:
- this_dir=`pwd`
- file_name=`basename $0`
- dirname $0|grep "^/" >/dev/null
- if [ $? -eq 0 ];then
- this_dir=`dirname $0`
- else
- dirname $0|grep "^\.\." >/dev/null
- retval=$?
- if [ $retval -eq 0 ];then
- this_dir=`dirname $0|sed "s#^#$this_dir/#"`
- else
- dirname $0|grep "^\./" >/dev/null
- retval=$?
- if [ $retval -eq 0 ];then
- this_dir=`dirname $0|sed "s#^\.\(/.*\)#$this_dir\1#"`
- else
- this_dir=`dirname $0|sed "s#^#$this_dir/#"`
- fi
- fi
- fi
- echo $this_dir
----------------------------------------------
功能:获取脚本的绝对路径。
测试:
[root@ppc40 java]# pwd
/tmp/whuang/study/java
[root@ppc40 java]# sh a.sh
/tmp/whuang/study/java
[root@ppc40 java]# cd ..
[root@ppc40 study]# sh java/a.sh
/tmp/whuang/study/java
[root@ppc40 study]# sh ./java/a.sh
/tmp/whuang/study/java
[root@ppc40 study]# sh ../study/java/a.sh
/tmp/whuang/study/../study/java
时间: 2025-01-28 08:44:55