symlink.sh (905B)
1 #!/bin/sh 2 # 3 # symlink 4 5 follow_link() { 6 # no file provided 7 [ "${1:-}" ] || return 1 8 9 file="${1}" 10 11 [ -e "${file%/}" ] || file=${file%"${file##*[!/]}"} 12 [ -d "${file:-/}" ] && file="${file}/" 13 14 while :; do 15 if [ ! "${file}" = "${file%/*}" ]; then 16 # follow with cd 17 case "${file}" in 18 /*) cd -P "${file%/*}/" 2>/dev/null || break ;; 19 *) cd -P "./${file%/*}" 2>/dev/null || break ;; 20 esac 21 file=${file##*/} 22 fi 23 24 # if not link print the actual file 25 if [ ! -L "${file}" ]; then 26 file="${PWD%/}${file:+/}${file}" 27 printf '%s\n' "${file:-/}" 28 break 29 fi 30 31 # use ls -dl to follow link 32 link="$(ls -dl -- "${file}" 2>/dev/null)" || break 33 file="${link#*" ${file} -> "}" 34 done 35 } 36 37 main() { follow_link "${@}" ; } 38 39 main "${@}"