下面是一個最簡單的實現(xiàn),可以解決大多數(shù)問題,缺陷是對于軟鏈接顯示的是軟鏈接所在的目錄
lineos:false
代碼如下:
#!/bin/bash
DIR="$( cd "$( dirname "$0" )" && pwd )"
完善版
這個版本解決了使用ln -s target linkName創(chuàng)造軟鏈接無法正確取到真實腳本的問題。
lineos:false
代碼如下:
#!/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 )"
更多信息請查看IT技術(shù)專欄