获取shell脚本自身所在目录的Shell脚本分享
简单版
下面是一个最简单的实现,可以解决大多数问题,缺陷是对于软链接显示的是软链接所在的目录
#!/bin/bash DIR="$( cd "$( dirname "$0" )" && pwd )"
完善版
这个版本解决了使用ln -s target linkName创造软链接无法正确取到真实脚本的问题。
#!/bin/bash SOURCE="$0" while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" SOURCE="$(readlink "$SOURCE")" [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located done DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
相关推荐
wozijisunfly 2020-05-28
LychieFan 2020-05-18
wangrui0 2020-04-26
linuxalienyan 2020-04-10
chenchuang 2020-03-05
lqp 2020-03-01
神龙 2020-03-01
fenxinzi 2020-01-11
jackadmi 2020-01-08
hickwu 2020-01-05
csdnyasin 2019-12-09
jackalwb 2019-12-08
jiangtie 2019-12-06
fenxinzi 2019-12-07
jackalwb 2019-11-10
86590798 2015-06-18
ohyeahhhh 2019-11-04