Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  shell  >  正文 获取shell脚本所在绝对路径操作方法介绍

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

发布时间:2014-09-02   编辑:www.jquerycn.cn
获取shell脚本所在绝对路径操作方法介绍

本文为大家介绍linux在shell脚本中获取该脚本的所在绝对路径的操作方法,供大家学习参考。
文件名:a.sh
位置:/tmp/whuang/study/php
脚本内容:
 

复制代码 代码如下:
#!/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 php]# pwd
/tmp/whuang/study/php
[root@ppc40 php]# sh a.sh
/tmp/whuang/study/php
[root@ppc40 php]# cd ..
[root@ppc40 study]# sh php/a.sh
/tmp/whuang/study/php
[root@ppc40 study]# sh ./php/a.sh
/tmp/whuang/study/php

您可能感兴趣的文章:
获取shell脚本所在绝对路径操作方法介绍
bash shell获取当前正执行脚本的绝对路径
bash shell脚本执行的几种方法
PHP 获取文件路径(__FILE__的妙用)
Python如何获取当前脚本绝对路径
取得当前shell目录的方法
inux shell初级入门教程
通过__FILE__灵活获取文件路径的方法
source命令执行shell文件与shell script文件名直接运行的区别
实现vi/cp/mv前自动备份源文件的shell脚本

[关闭]