From d29ee6f73da53fc809ba48a1d0979c0b1721510f Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 25 Nov 2016 00:27:39 +0000 Subject: [PATCH] compatible busybox --- etc/profile | 53 +++++++++++++++++++++++--------- etc/profile.d/aliases | 8 ----- etc/profile.d/docker.sh | 5 ++- etc/profile.d/functions | 68 +++++++++++++++++++++++------------------ etc/profile.d/git.sh | 6 ++-- etc/profile.d/nb.sh | 55 ++++++++++++++++++++++++++++++++- 6 files changed, 137 insertions(+), 58 deletions(-) diff --git a/etc/profile b/etc/profile index 162e7912..83d96c1c 100755 --- a/etc/profile +++ b/etc/profile @@ -7,7 +7,7 @@ #NB_DEBUG='echo ' if [ -n "$NB_LOOP" ]; then [ -n "$NB_DEBUG" ] && $NB_DEBUG "WARN: NB_LOOP=$NB_LOOP: BASH_SOURCE=$BASH_SOURCE \$0=$0" - echo "WARN: NB_LOOP=$NB_LOOP: BASH_SOURCE=$BASH_SOURCE \$0=$0" + echo "WARN: NB_LOOP=$NB_LOOP: BASH_SOURCE=$BASH_SOURCE \$0=$0 | Won't load !" NB_LOOP=$(($NB_LOOP+1)) unset NB_LOOP return 2>/dev/null || exit # if executable @@ -20,33 +20,54 @@ export NB_LOOP=1 # # SEARCH ROOT # -CURRENT_DIR='' +NB_CURRENT_DIR='' #[ "${BASH-no}" != "no" ] && echo Is BASH if [ -n "$BASH_SOURCE" ]; then - #CURRENT_DIR="${BASH_SOURCE%/*}" - CURRENT_DIR="${BASH_SOURCE%/*}" + #NB_CURRENT_DIR="${BASH_SOURCE%/*}" + NB_CURRENT_DIR="${BASH_SOURCE%/*}" + elif [ -n "$ZSH_VERSION" ]; then - eval 'CURRENT_DIR=${(%):-%N}' - CURRENT_DIR=${dir%/*} + eval 'NB_CURRENT_DIR=${(%):-%N}' + NB_CURRENT_DIR=${dir%/*} + elif [ "$KSH_VERSION" != "" ]; then #elif eval '[[ -n ${.sh.file} ]]' 2>/dev/null; then - eval 'CURRENT_DIR="$(dirname "${.sh.file}")"' + eval 'NB_CURRENT_DIR="$(dirname "${.sh.file}")"' + elif [ -r /etc/profile.d/nb.sh ]; then - CURRENT_DIR=$(dirname $(grep '\. ' -m1 /etc/profile.d/nb.sh | cut -d'.' -f2 /etc/profile.d/nb.sh)) + NB_CURRENT_DIR=$(dirname $(grep '\. ' -m1 /etc/profile.d/nb.sh | cut -d'.' -f2 /etc/profile.d/nb.sh)) + elif [ -r "$0" ]; then - CURRENT_DIR="$(dirname "$0")" + NB_CURRENT_DIR="$(dirname "$0")" + else - for i in /*/*/bin/nb-install /*/*/*/bin/nb-install; do - [ -r "$i" ] && CURRENT_DIR=$(dirname "$i")/../etc && break + # Alpine, busybox, ... + for i in /*/*/etc/profile.d/functions /*/*/*/etc/profile.d/functions; do + cd "$(dirname "$i")"/.. &> /dev/null || continue + [ -e profile.d/envs ] || continue + NB_CURRENT_DIR=`pwd` || unset NB_CURRENT_DI + break + #[ -r "$i" ] && NB_CURRENT_DIR=$(dirname "$i")/../../.. && break done +# NB 24.11.16 for i in /*/*/bin/nb-install /*/*/*/bin/nb-install; do +# NB 24.11.16 ls $(dirname "$i")/../..; break +# NB 24.11.16 break +# NB 24.11.16 [ -r "$i" ] && NB_CURRENT_DIR=$(dirname "$i")/../.. && break +# NB 24.11.16 done + fi -if [ -z "$CURRENT_DIR" ]; then +if [ -z "$NB_CURRENT_DIR" ]; then + export NB_UNSUPORTED=1 [ -n "$NB_DEBUG" ] && $NB_DEBUG "ERR: Unsupported shell. Please use bash, ksh93 or zsh." + else + unset NB_UNSUPORTED + #echo $NB_CURRENT_DIR; return - [ -e "$CURRENT_DIR/profile.d/functions" ] && . "$CURRENT_DIR/profile.d/functions" - export NB_ROOT=$(realpath $CURRENT_DIR/.. 2>/dev/null) + [ -e "$NB_CURRENT_DIR/profile.d/functions" ] && . "$NB_CURRENT_DIR/profile.d/functions" + export NB_ROOT=$(realpath $NB_CURRENT_DIR/.. 2>/dev/null) + [ -n "$NB_DEBUG" ] && $NB_DEBUG "NB_ROOT=$NB_ROOT" # # Profiles @@ -54,6 +75,7 @@ else if [ -n "$NB_ROOT" ]; then [ -e "$NB_ROOT/etc/profile.d/envs" ] && . "$NB_ROOT/etc/profile.d/envs" + #${NB_ROOT}/etc/profile.d/*.sh for i in \ ${NB_ROOT}/etc/profile.d/aliases \ $(nb-repo-dirs "etc/profile" "etc/profile.d/*.sh" | grep -vFx "${NB_ROOT}/etc/profile") \ @@ -66,4 +88,5 @@ else fi -unset i tmp CURRENT_DIR NB_DEBUG NB_LOOP +#export NB_LASTTIME=$(date +"%Y-%M-%d %T") +unset i tmp NB_CURRENT_DIR NB_DEBUG NB_LOOP diff --git a/etc/profile.d/aliases b/etc/profile.d/aliases index 692bf91a..77f4065e 100644 --- a/etc/profile.d/aliases +++ b/etc/profile.d/aliases @@ -53,14 +53,6 @@ test "$TERM_PROGRAM" = "iTerm.app" && export PROMPT_COMMAND='printf "\033]0;%s\0 alias Get='curl -sk' alias Head='curl -sk --head' alias myip='Get http://nicolas.boisselier.free.fr/myip/' -# NB 15.07.15 function nbGet() { -# NB 15.07.15 declare host port uri -# NB 15.07.15 eval $(perl -ne 'm,^\s*\w+://([^/:]+)(?::(\d+))?(/.*)?\s*$, and print qq|host=$1 port=$2 uri=$3\n|' <<< "$@") -# NB 15.07.15 [ -z "$port" ] && port=80 -# NB 15.07.15 [ -z "$uri" ] && uri='/' -# NB 15.07.15 echo $host $port $uri -# NB 15.07.15 printf "GET $uri HTTP/1.0\r\nHost: $host\r\n\r\n" |nc -i 5 $host $port -# NB 15.07.15 } #-------------------------------------------------------------------------------- # bc diff --git a/etc/profile.d/docker.sh b/etc/profile.d/docker.sh index 346d66c1..6dd5460c 100644 --- a/etc/profile.d/docker.sh +++ b/etc/profile.d/docker.sh @@ -7,9 +7,12 @@ which docker > /dev/null || return 0 [ -d ~/ownCloud/Docker ] && export DOCKER_HOME=~/ownCloud/Docker [ -z "$DOCKER_HUB" ] && DOCKER_HUB="" +# Docker env only if `is_prompt` !!! if is_prompt && which docker-machine > /dev/null && [ -z "$MYVIMRC" ]; then - if [ -z "$DOCKER_CERT_PATH" -o ! -e "$DOCKER_CERT_PATH" ] && docker-machine ls -q 2>/dev/null |grep -qFm1 "$DOCKER_MACHINE_NAME"; then + if [ -z "$DOCKER_CERT_PATH" -o ! -e "$DOCKER_CERT_PATH" ] \ + && [ _$(docker-machine status "$DOCKER_MACHINE_NAME" 2>/dev/null) = _Running ] \ + ; then eval "$(docker-machine env "$DOCKER_MACHINE_NAME")" 2>/dev/null fi diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 908832ae..ea29f14f 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -17,7 +17,7 @@ shell_help() { # local msg="$1"; shift - declare i + local i #while [ $# -gt 0 ]; do for i in $@; do case "$i" in @@ -54,16 +54,16 @@ shell_functions() { } shell_ssh() { - declare ssh_opt; case "$1" in -*) ssh_opt="$1"; shift ;; esac - declare server; server="$1"; shift - declare tmp; tmp="/tmp/$FUNCNAME.$USER" + local ssh_opt; case "$1" in -*) ssh_opt="$1"; shift ;; esac + local server; server="$1"; shift + local tmp; tmp="/tmp/$FUNCNAME.$USER" ( alias | sed -e 's/^alias //' -e 's/^/alias /' eval type $(shell_functions|perl -pe 's/\s+/ /') | grep -v ' function$' echo "[ -r /etc/profile ] && source /etc/profile" ) | ssh "$server" "cat > $tmp || rm -vf $tmp" || return - declare args + local args if [ -z "$@" ]; then [ -z "$ssh_opt" ] && ssh_opt='-t' @@ -87,7 +87,7 @@ ips() { env_add_path() { # Add paths to a variables # Usage PATH=`env_add_path "$PATH" "/blbabla"` - declare env_value=$1; shift + local env_value=$1; shift for p in $@; do [ -e "$p" ] || continue @@ -104,7 +104,16 @@ env_add_path() { } realpath() { - perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n" if -e $ARGV[0]' $1 + if [ -x /usr/bin/realpath ]; then + /usr/bin/realpath "$1" + elif which perl &> /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*) readlinks "$1" ;; + *) readlinks -f "$1" ;; + esac + fi } ascii() { @@ -120,7 +129,7 @@ ascii() { } psmem() { - declare ps; ps=`ps ax -O rss 2>/dev/null` + local ps=`ps ax -O rss 2>/dev/null` echo "$ps"| awk "\ BEGIN { count = 0; sum= 0; } /^ *[0-9]+/ && /$1/ { count ++; sum += \$2 } @@ -135,7 +144,7 @@ END { } unzipurl() { - declare url file + local url file url=$1 file=`echo "$url"|awk -F/ '/\.zip$/ { print $NF }'` [ -z "$file" ] && file="$FUNCNAME.zip" @@ -156,7 +165,7 @@ while (<>) { } bytes2h() { - declare in args + local in args if [ "$1" = "-regex" ]; then args="regex=$2" @@ -202,8 +211,8 @@ ls_tree() { } replace_tag_content() { - declare tag; tag="$1"; shift - declare content; content="$@" + local tag; tag="$1"; shift + local content; content="$@" perl -ne 'BEGIN { $tag = shift(@ARGV)." - replace_tag_content"; $t1 = "# >>> $tag"; @@ -225,7 +234,7 @@ END { print "$content" if $content; } _getexp_pcregrep="" getexp() { [ -z "$*" -o "$1" == "-h" -o "$1" == "--help" ] && echo "Usage: $FUNCNAME [capture number|default 0] PERL_REGEXP FILES..." && return - declare exp num + local exp num num=0; case "$1" in [0-9]*) num=$1; shift ;; esac exp=$1; shift @@ -278,8 +287,8 @@ exec "'$prog'",$_[1]; ls_img_black() { - declare threshold; threshold=10 - declare ret; ret=1 + local threshold; threshold=10 + local ret; ret=1 shell_help_noarg "Usage: $FUNCNAME [-threshold PCENT (default: $threshold)] IMG1 IMG2 ..." "$@" && return 1 @@ -309,12 +318,12 @@ img2pdf() { jpg2avi() { shell_help_noarg "Usage: $FUNCNAME [-h] JPG_FILES AVI_FILE" "$@" && return - declare still; still=/tmp/$FUNCNAME.$$.txt; [ -x /dev/shm ] && tmp=/dev/shm/$FUNCNAME.$$.txt + local still; still=/tmp/$FUNCNAME.$$.txt; [ -x /dev/shm ] && tmp=/dev/shm/$FUNCNAME.$$.txt - declare avi jpg + local avi jpg - declare tot; tot=$# - declare count; count=0 + local tot; tot=$# + local count; count=0 while [ $# -gt 0 ]; do count=$(($count+1)) @@ -328,14 +337,14 @@ jpg2avi() { done #echo $(cat $still)" -> $avi" - declare opt='' + local opt='' [ "$avi" == "-" ] && opt="$opt -really-quiet" mencoder${opt} -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=1920:1080 -mf type=jpeg:fps=24 mf://@${still} -o $avi rm -f "$still" } cron_d_install() { - declare usage fname file exp cron tmp test + local usage fname file exp cron tmp test usage="Usage: $FUNCNAME [-t|--test] [CRON_D_FILE] [IGNORE_REGEXP] or [NAME] [CRON_D_CONTENT] [IGNORE_REGEXP]" shell_help_noarg "$usage" "$@" && return 1 @@ -433,10 +442,10 @@ exit $diff ? 0 : 1; csv2human() { shell_help "Usage: $FUNCNAME [PERL_REGEXP --sep|-s (default: tab)] [--noheader|-nh]" "$@" && return; - declare sep noheader; + local sep noheader; sep='\t'; noheader=0; - declare files='' + local files='' while [ $# -gt 0 ]; do @@ -528,9 +537,8 @@ shell_push_arg() { urlencode() { local length="${#1}" local i - for (( i = 0; i < length; i++ )); do - #for i in `seq 0 $(($length-1))`; do - local c="${1:i:1}" + for i in `seq 0 $(($length-1))`; do + eval 'local c="${1:i:1}"' # TODO compatible with busybox case $c in [a-zA-Z0-9.~_-]) printf "$c" ;; *) printf '%%%02X' "'$c" @@ -555,7 +563,7 @@ boot_loader() { return 0 } -function is_empty() { [[ -z $(ls -A "/$1" | head -1) ]]; } +is_empty() { [[ -z $(ls -A "/$1" | head -1) ]]; } is_prompt() { #[[ $- == *i* ]] && echo "Interactive" || echo "Not interactive" @@ -591,7 +599,7 @@ url2nc() { } -function ls_users() { +ls_users() { case "$OSTYPE" in darwin*) dscacheutil -q user \ @@ -613,8 +621,8 @@ function ls_users() { esac } -function reverse() { perl -e 'print reverse <>'; } -function epoch2date() { perl -M'POSIX strftime' -e 'e2d($ARGV[0]) and print "\n" and exit if @ARGV; while(<>){e2d($_)}; sub e2d(){local $_=shift;s/^(\d+)/strftime("%Y-%m-%d %H:%M:%S",localtime($1))/e; print $_}' $@; } +reverse() { perl -e 'print reverse <>'; } +epoch2date() { perl -M'POSIX strftime' -e 'e2d($ARGV[0]) and print "\n" and exit if @ARGV; while(<>){e2d($_)}; sub e2d(){local $_=shift;s/^(\d+)/strftime("%Y-%m-%d %H:%M:%S",localtime($1))/e; print $_}' $@; } #function epoch2date() { perl -M'POSIX strftime' -e 'e2d while<>; sub e2d(){local $_=shift;s/^(\d+)/strftime("%Y-%m-%d %H:%M:%S",localtime($1))/e; print $_}' $@; } find_sort_mtime() { diff --git a/etc/profile.d/git.sh b/etc/profile.d/git.sh index a3c350e2..a2d82381 100644 --- a/etc/profile.d/git.sh +++ b/etc/profile.d/git.sh @@ -77,8 +77,8 @@ git_commit_push_add() { git_create() { shell_help "Usage: $FUNCNAME create git project from current dir" "$@" && return 1 - declare dir="$(basename $(pwd))" - declare repo=$(echo "$GIT_REPO" | perl -pe 's,^\w+://,,; s,/.*$,,;') + local dir="$(basename $(pwd))" + local repo=$(echo "$GIT_REPO" | perl -pe 's,^\w+://,,; s,/.*$,,;') [ -n "$1" ] && repo="$1" && shift # @@ -109,7 +109,7 @@ git_create() { } git_clone() { - declare repo=$GIT_REPO + local repo=$GIT_REPO case "$repo" in *"//"*) ;; *) repo="$repo:" diff --git a/etc/profile.d/nb.sh b/etc/profile.d/nb.sh index 86e61e1e..c47aa775 100644 --- a/etc/profile.d/nb.sh +++ b/etc/profile.d/nb.sh @@ -1,3 +1,56 @@ +nb_repo() { +# NB 24.11.16: TODO +if false && which perl &> /dev/null; then + nb-repo-dirs $@ +else + + if [ -z "$*" ]; then + echo "$NB_ROOT" + ( + ls -d1 ` \ + /*/*/.nb-instal \ + /usr/local/src/*/.nb-install \ + + /home/*/*/.nb-instal \ + /Users/*/*/.nb-instal \ + + /home/*/git/*/.nb-instal \ + /Users/*/git/*/.nb-instal \ + + /home/*/app/*/.nb-instal \ + /Users/*/app/*/.nb-instal \ + + /home/*/code/*/.nb-instal \ + /Users/*/code/*/.nb-instal \ + + ` \ + | xargs -n1 dirname \ + | xargs -n1 realpath \ + | xargs -n1 uniq -c \ + ) 2>/dev/null +# NB 24.11.16 /{opt,opt/www,etc,home,home/www}/*/.nb-{install,update} +# NB 24.11.16 /usr/local/src/.nb-install +# NB 24.11.16 /{home,Users}/*/*/.nb-install + local conf="$NB_ROOT/etc/repo-dirs.conf" + #[ -r "$conf" ] || return + else + local ifs_bak="$IFS"; IFS=$'\n' + local d f i repo +#eval 'ls /opt/nb/etc/profile.d/*.sh' + for d in `$FUNCNAME`; do + #for d in "$NB_ROOT"; do + for f in "$@"; do + for repo in $(ls -d $d/$f); do + echo "$repo" + done + done + done + IFS="$ifs_bak" + fi + +fi +} + nb_repos_commit_push() { shell_help "Commit each repos whith a comment" "$@" && return local status @@ -95,7 +148,7 @@ nb_alert() { *) host=macbook.brighton.loc case $HOSTNAME in *[Mm][Aa][Cc][Bb][Oo][Oo][Kk]*) host="";; esac - local msg="$FUNCNAME from $HOSTNAME "$(tr "\"\`'" "”"<<< "$@") + local msg="$FUNCNAME from $HOSTNAME "$(echo "$*" | tr "\"\`'" "”") cmd="$cmd && mac_xmessage \"$msg\" 1>/dev/null" ;; esac -- 2.47.3