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

前端 未结 12 688
粉色の甜心
粉色の甜心 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:58

    The variable $RPATH contains the relative path to the real file or the real path for a real file.

    CURPATH=$( cd -P -- "$(dirname -- "$(command -v -- "$0")")" && pwd -P )
    
    CURLOC=$CURPATH/`basename $0`
    
    if [ `ls -dl $CURLOC |grep -c "^l" 2>/dev/null` -ne 0 ];then
    
        ROFFSET=`ls -ld $CURLOC|cut -d ">" -f2 2>/dev/null`
    
        RPATH=`ls -ld $CURLOC/$ROFFSET 2>/dev/null`
    
    else
    
        RPATH=$CURLOC
    
    fi
    
    echo $RPATH
    

提交回复
热议问题