- A+
所属分类:Shell
使用pwd 命令获取的是执行该命令的当前工作目录,当在其他目录调用一个脚本时会发现脚本中使用的pwd命令获取的结果不是脚本所在的绝对
一段代码获取
1 |
base_dir=$(cd "$(dirname "$0")";pwd) |
大脚本获取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#!/bin/sh this_dir=`pwd` echo "$this_dir ,this is pwd" echo "$0 ,this is \$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 this_dir=`dirname $0|sed "s#^#$this_dir/#"` fi fi echo $this_dir |
转自:http://www.zhengdazhi.com/?p=139

微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
23/03/2015 下午 6:45 沙发
一般使用:
cd — "$(dirname — "$0")"