获取shell脚本目录

#!/usr/bin/env bash

BASE_DIR="$(pwd)"

function get_script_dir {
# resolve links - $0 may be a softlink
PRG="$0"
while [ -h "$PRG" ]; do #-h 文件存在并且是一个字符链接
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`/"$link"
fi
done
PRGDIR=`dirname "$PRG"`
pushd $PRGDIR &> /dev/null
BASE_DIR=$(pwd)
popd &> /dev/null
}

# get script's directory
get_script_dir

 

 

posted @ 2019-05-29 10:05  那个谁866  阅读(564)  评论(0编辑  收藏  举报