我可以在KornShell中获取当前脚本的绝对路径吗?

问题描述:

是否可以找到KornShell(ksh)中当前正在执行的脚本的完整路径?

Is it possible to find out the full path to the script that is currently executing in KornShell (ksh)?

即如果我的脚本在/opt/scripts/myscript.ksh中,我可以在该脚本中以编程方式发现/opt/scripts/myscript.ksh吗?

i.e. if my script is in /opt/scripts/myscript.ksh, can I programmatically inside that script discover /opt/scripts/myscript.ksh ?

谢谢

您可以使用:

## __SCRIPTNAME - name of the script without the path
##
typeset -r __SCRIPTNAME="${0##*/}"

## __SCRIPTDIR - path of the script (as entered by the user!)
##
__SCRIPTDIR="${0%/*}"

## __REAL_SCRIPTDIR - path of the script (real path, maybe a link)
##
__REAL_SCRIPTDIR=$( cd -P -- "$(dirname -- "$(command -v -- "$0")")" && pwd -P )