How can I get the behavior of GNU's readlink

On Linux, the readlink utility accepts an option -f that follows additional links. This doesn't seem to work on Mac and possibly BSD based systems. What would the equivalent be?

Here's some debug information:

$ which readlink; readlink -f
/usr/bin/readlink
readlink: illegal option -f
usage: readlink [-n] [file ...]

readlink -f does two things:

  • It iterates along a sequence of symlinks until it finds an actual file.
  • It returns that file's canonicalized name—ie, its absolute pathname.
  • If you want to, you can just build a shell script that uses vanilla readlink behavior to achieve the same thing. Here's an example. Obviously you could insert this in your own script where you'd like to call readlink -f

    #!/bin/sh
    
    TARGET_FILE=$1
    
    cd `dirname $TARGET_FILE`
    TARGET_FILE=`basename $TARGET_FILE`
    
    # Iterate down a (possible) chain of symlinks
    while [ -L "$TARGET_FILE" ]
    do
        TARGET_FILE=`readlink $TARGET_FILE`
        cd `dirname $TARGET_FILE`
        TARGET_FILE=`basename $TARGET_FILE`
    done
    
    # Compute the canonicalized name by finding the physical path 
    # for the directory we're in and appending the target file.
    PHYS_DIR=`pwd -P`
    RESULT=$PHYS_DIR/$TARGET_FILE
    echo $RESULT
    

    Note that this doesn't include any error handling. Of particular importance, it doesn't detect symlink cycles. A simple way to do this would be to count the number of times you go around the loop and fail if you hit an improbably large number, such as 1,000.

    EDITED to use pwd -P instead of $PWD .

    Note that this script expects to be called like ./script_name filename , no -f , change $1 to $2 if you want to be able to use with -f filename like GNU readlink.


    MacPorts and Homebrew provide a coreutils package containing greadlink (GNU readlink). Credit to Michael Kallweitt post in mackb.com.

    brew install coreutils
    
    greadlink -f file.txt
    

    You may be interested in realpath(3) , or Python's os.path.realpath . The two aren't exactly the same; the C library call requires that intermediary path components exist, while the Python version does not.

    $ pwd
    /tmp/foo
    $ ls -l
    total 16
    -rw-r--r--  1 miles    wheel  0 Jul 11 21:08 a
    lrwxr-xr-x  1 miles    wheel  1 Jul 11 20:49 b -> a
    lrwxr-xr-x  1 miles    wheel  1 Jul 11 20:49 c -> b
    $ python -c 'import os,sys;print(os.path.realpath(sys.argv[1]))' c
    /private/tmp/foo/a
    

    I know you said you'd prefer something more lightweight than another scripting language, but just in case compiling a binary is insufferable, you can use Python and ctypes (available on Mac OS X 10.5) to wrap the library call:

    #!/usr/bin/python
    
    import ctypes, sys
    
    libc = ctypes.CDLL('libc.dylib')
    libc.realpath.restype = ctypes.c_char_p
    libc.__error.restype = ctypes.POINTER(ctypes.c_int)
    libc.strerror.restype = ctypes.c_char_p
    
    def realpath(path):
        buffer = ctypes.create_string_buffer(1024) # PATH_MAX
        if libc.realpath(path, buffer):
            return buffer.value
        else:
            errno = libc.__error().contents.value
            raise OSError(errno, "%s: %s" % (libc.strerror(errno), buffer.value))
    
    if __name__ == '__main__':
        print realpath(sys.argv[1])
    

    Ironically, the C version of this script ought to be shorter. :)

    链接地址: http://www.djcxy.com/p/9730.html

    上一篇: 获取当前脚本的绝对路径

    下一篇: 我怎样才能得到GNU的readlink的行为