From 7c67fe0b1e8f874409a74b43576dca7836331b83 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 11 Mar 2019 06:31:59 +0000 Subject: [PATCH] etc/profile.d/functions --- etc/profile.d/functions | 76 +++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 82c05f49..e95eda6c 100755 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -21,25 +21,43 @@ which timeout > /dev/null || timeout() { fi } -which realpath > /dev/null || realpath() { - perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n" if -e $ARGV[0]' "$1" - return - case "$OSTYPE" in - darwin*) [ -h "$1" ] && readlink "$1" || echo "$1";; - *) readlink -f "$1" ;; - esac +which realpath2 > /dev/null || realpath2() { + local pwd_bak=$PWD + + [ -e "$1" ] || return + + if [ -d "$1" ]; then + cd "$1" || return + readlink "$PWD" || echo $PWD + else + + local BASENAME="$(basename "$1")" + local LINK=$(readlink "$BASENAME") + cd "$(dirname "$1")" + + while [ "$LINK" ]; do + cd "$(dirname "$LINK")" + LINK=$(readlink "$BASENAME") + done + + if [ "$PWD" = "/" ]; then + echo "$PWD$BASENAME" + else + echo "$PWD/$BASENAME" + fi + + fi + + cd "$pwd_bak" } -# NB 11.03.19 which realpath > /dev/null || realpath() { -# NB 11.03.19 if which perl 1>/dev/null; then -# NB 11.03.19 perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n" if -e $ARGV[0]' "$1" -# NB 11.03.19 else -# NB 11.03.19 case "$OSTYPE" in -# NB 11.03.19 darwin*) readlink "$1" ;; -# NB 11.03.19 *) readlink -f "$1" ;; -# NB 11.03.19 esac -# NB 11.03.19 fi -# NB 11.03.19 } +if ! which realpath > /dev/null; then + if which grealpath > /dev/null; then + realpath() { grealpath $@; } + else + realpath() { perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n" if -e $ARGV[0]' "$1"; } + fi +fi which seq > /dev/null || seq() { local begin=1 @@ -61,30 +79,6 @@ which seq > /dev/null || seq() { awk -v begin=$begin -v end=$end -v step=$step 'BEGIN{for(i=begin;i<=end;i=i+step) {print i}}' } -nb_repos2() { - ( - # Always ! - echo "$NB_ROOT" - - # Conf file - local conf="/etc/nb/repos.conf" - [ -s "$conf" ] || conf="$NB_ROOT/etc/repos.conf" - - # Realpath - eval ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null | while read dir; do - - # Accept directories or files (ex: use to tag a directory: .nb-install) - [ -f "$dir" ] && dir=$(dirname "$dir") - - dir=$(realpath2 "$dir") - echo "$dir" - - done - - # no doubles - # strong if awk failed - ) | awk \!'x[$0]++' || echo "$NB_ROOT" -} nb_repos() { [ -z "$FUNCNAME" ] && local FUNCNAME='nb_repos' # for busybox ( -- 2.47.3