#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
#
# 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
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") \
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
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
[ -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
#
local msg="$1"; shift
- declare i
+ local i
#while [ $# -gt 0 ]; do
for i in $@; do
case "$i" in
}
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'
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
}
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() {
}
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 }
}
unzipurl() {
- declare url file
+ local url file
url=$1
file=`echo "$url"|awk -F/ '/\.zip$/ { print $NF }'`
[ -z "$file" ] && file="$FUNCNAME.zip"
}
bytes2h() {
- declare in args
+ local in args
if [ "$1" = "-regex" ]; then
args="regex=$2"
}
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";
_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
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
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))
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
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
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"
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"
}
-function ls_users() {
+ls_users() {
case "$OSTYPE" in
darwin*)
dscacheutil -q user \
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() {
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
#
}
git_clone() {
- declare repo=$GIT_REPO
+ local repo=$GIT_REPO
case "$repo" in
*"//"*) ;;
*) repo="$repo:"
+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
*)
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