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

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

    How the script was called is stored in the variable $0. You can use readlink to get the absolute file name:

    readlink -f "$0"
    
    0 讨论(0)
  • 2020-12-29 06:56

    readlink -f would be the best if it was portable, because it resolves every links found for both directories and files.

    On mac os x there is no readlink -f (except maybe via macports), so you can only use readlink to get the destination of a specific symbolic link file.

    The $(cd -P ... pwd -P) technique is nice but only works to resolve links for directories leading to the script, it doesn't work if the script itself is a symlink

    Also, one case that wasn't mentioned : when you launch a script by passing it as an argument to a shell (/bin/sh /path/to/myscript.sh), $0 is not usable in this case

    I took a look to mysql "binaries", many of them are actually shell scripts ; and now i understand why they ask for a --basedir option or need to be launched from a specific working directory ; this is because there is no good solution to locate the targeted script

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-29 06:59

    Try which command.

    which scriptname
    

    will give you the full qualified name of the script along with its absolute path

    0 讨论(0)
  • 2020-12-29 07:04

    I upgraded the Edward Staudt's answer, to be able to deal with absolute-path symbolic links, and with chains of links too.

    DZERO=$0
    while true; do
      echo "Trying to find real dir for script $DZERO"
      CPATH=$( cd -P -- "$(dirname -- "$(command -v -- "$DZERO")")" && pwd -P )
      CFILE=$CPATH/`basename $DZERO`
      if [ `ls -dl $CFILE | grep -c "^l" 2>/dev/null` -eq 0 ];then
        break
      fi
      LNKTO=`ls -ld $CFILE | cut -d ">" -f2 | tr -d " " 2>/dev/null`
      DZERO=`cd $CPATH ; command -v $LNKTO`
    done
    

    Ugly, but works... After run this, the path is $CPATH and the file is $CFILE

    0 讨论(0)
  • 2020-12-29 07:05

    This is what I did:

    if [[ $0 != "/"* ]]; then
      DIR=`pwd`/`dirname $0`
    else
      DIR=`dirname $0`
    fi
    
    0 讨论(0)
提交回复
热议问题