From 017909002333f9c586f1e124f85018a5d5c4d469 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 11 Mar 2019 05:13:42 +0000 Subject: [PATCH] etc/profile.d/functions --- etc/profile.d/functions | 61 +++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 4cf13d56..ec5fdca6 100755 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -4,6 +4,7 @@ # ################################################################################# type -P which > /dev/null || which() { type -P "$1" 2>&1; } + which nproc > /dev/null || nproc() { case "$OSTYPE" in darwin*) sysctl -n hw.ncpu ;; @@ -20,6 +21,25 @@ which timeout > /dev/null || timeout() { fi } +which realpath > /dev/null || realpath() { + case "$OSTYPE" in + darwin*) [ -h "$1" ] && readlink "$1" || echo "$1";; + *) readlink -f "$1" ;; + esac + # perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n" if -e $ARGV[0]' "$1" +} + +# 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 } + which seq > /dev/null || seq() { local begin=1 local end @@ -40,6 +60,30 @@ 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 ( @@ -51,10 +95,6 @@ nb_repos() { [ -s "$conf" ] || conf="$NB_ROOT/etc/repos.conf" # Realpath - #ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null -# NB 22.02.19 local IFS=" -# NB 22.02.19 " -# NB 22.02.19 for dir in $(eval ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null); do 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) @@ -222,19 +262,6 @@ env_add_path() { [ -z "$env_value" ] || echo "$env_value" } -if ! which realpath >/dev/null; then -realpath() { - if which perl 1>/dev/null; then - perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n" if -e $ARGV[0]' "$1" - else - case "$OSTYPE" in - darwin*) readlink "$1" ;; - *) readlink -f "$1" ;; - esac - fi -} -fi - ascii() { if [ ! -t 0 ]; then -- 2.47.3