Article Listing 1 Listing 2 Listing 3 Listing 4
Listing 5 Listing 6 Listing 7 Listing 8 Listing 9
Listing 10 Listing 11 Listing 12 Listing 13 Sidebar aug2004.tar

Listing 11 get_link_target

{
    # Echos the target filename that a symbolic link references. Returns 0
    # if $1 is a symbolic link; otherwise, returns 1.

    local FUNC_NAME=get_link_target
    ${TRACE:-trace $FUNC_NAME $@}

    local link=$1
    local target

    if [[ -L $link ]]
    then
        line=$(ls -l $link 2>&-)
        target=${line##*'-> '}
        if [[ -n $target ]]
        then
            echo $target
            return 0
        else
            ${INFO:-info "race condition: $link disappeared or changed!"}
        fi
    fi
    ${ERROR:-error "$link is not a symbolic link"}
    return 1
}