Ticket #5852: realpath_bash.sh

File realpath_bash.sh, 501 bytes (added by jdemeyer, 10 years ago)

Shell script replacement for "readlink -f"

Line 
1#!/usr/bin/env bash
2
3realpath_bash()
4{
5    fname="${1%/}" # strips trailing '/'
6    while [ -L "$fname" ] ; do
7        dir="$(dirname "$fname")"
8        fname="$(command ls -l "$fname")"
9        fname="${fname#*\> }"
10        if [ "$fname" = "." ] ; then
11            fname="$dir"
12        elif echo "$fname" | grep -v '^/' > /dev/null ; then
13            fname="$dir/$fname"
14        fi
15    done
16    pushd "$(dirname "$fname")" > /dev/null
17    echo "$(pwd -P)/$(basename "$fname")"
18    popd > /dev/null
19}