获取所执行的程序所在目录及其上层目录的办法

抄自catalina.sh


PRG="$0"

# resolve links - $0 may be a softlink
while [ -h "$PRG" ]; do
  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"`

# 所执行程序所在的目录的上级目录 
[ -z "$CATALINA_HOME" ] && CATALINA_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.