linux获取shell脚本所在绝对路径操作介绍

软件发布|下载排行|最新软件

当前位置:首页操作系统Linux操作系统

linux获取shell脚本所在绝对路径操作介绍

  2020-12-11 我要评论
脚本名:a.sh
位置:/tmp/whuang/study/java
脚本内容:


#!/bin/sh
this_dir=`pwd`
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
this_dir=`dirname $0|sed "s#^#$this_dir/#"`
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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们