Can I get the absolute path to the current script in KornShell?

前端 未结 12 689
粉色の甜心
粉色の甜心 2020-12-29 06:26

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

i.e. if my script is in /opt/scripts/myscript.ksh

12条回答
  •  自闭症患者
    2020-12-29 06:52

    You could use:

    ## __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 )
    

提交回复
热议问题