From e148f1aba514834eb0bdbd748315900bb32c2b79 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Thu, 7 Dec 2017 03:02:57 +0000 Subject: [PATCH] changes --- etc/profile.d/functions | 1072 +++++++++++++++++----------------- etc/vim/source/functions.vim | 8 - etc/vim/vimplate-dump | 12 - 3 files changed, 537 insertions(+), 555 deletions(-) delete mode 100755 etc/vim/vimplate-dump diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 77036322..57480898 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -4,57 +4,57 @@ # ################################################################################# nb_repos() { - [ -z "$FUNCNAME" ] && local FUNCNAME='nb_repos' # for busybox - ( - # Always ! - echo "$NB_ROOT" - - # Conf file - local conf=/etc/nb/repos.conf - [ -s "$conf" ] || conf="$NB_ROOT/etc/repos.conf" - - # Realpath - #ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null - local IFS=" + [ -z "$FUNCNAME" ] && local FUNCNAME='nb_repos' # for busybox + ( + # Always ! + echo "$NB_ROOT" + + # Conf file + local conf=/etc/nb/repos.conf + [ -s "$conf" ] || conf="$NB_ROOT/etc/repos.conf" + + # Realpath + #ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null + local IFS=" " - for dir in $(eval ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null); do + for dir in $(eval ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null); do - # Accept directories or files (ex: use to tag a directory: .nb-install) - [ -f "$dir" ] && dir=$(dirname "$dir") + # Accept directories or files (ex: use to tag a directory: .nb-install) + [ -f "$dir" ] && dir=$(dirname "$dir") - dir=$(realpath "$dir") - echo "$dir" + dir=$(realpath "$dir") + echo "$dir" - done + done - # no doubles - # strong if awk failed - ) | awk \!'x[$0]++' || echo "$NB_ROOT" + # no doubles + # strong if awk failed + ) | awk \!'x[$0]++' || echo "$NB_ROOT" } nb_repos_ls() { - [ -z "$FUNCNAME" ] && local FUNCNAME='nb_repos_ls' # for busybox + [ -z "$FUNCNAME" ] && local FUNCNAME='nb_repos_ls' # for busybox - # Look for paths - local d f i repo - local IFS=" + # Look for paths + local d f i repo + local IFS=" " - for d in `nb_repos`; do + for d in `nb_repos`; do - for f in "$@"; do + for f in "$@"; do - #nb_debug "====$d/$f" - for repo in "$d"/$f; do - [ -e "$repo" ] && echo "$repo" - done + #nb_debug "====$d/$f" + for repo in "$d"/$f; do + [ -e "$repo" ] && echo "$repo" + done - done + done - done + done } nb_repos_grep() { - grep $@ `nb_repos` + grep $@ `nb_repos` } # NB 13.12.16: TODO replace with nb_repos and nb_repos_ls @@ -114,236 +114,236 @@ nb_repos_grep() { # NB 21.10.17 } timeout() { - if which timeout 1>/dev/null; then - timeout -- $@ - else - perl -e 'BEGIN{$|=1}; $SIG{ALRM}=sub{exit 1}; alarm(shift @ARGV); system join(" ",@ARGV)' -- $@ - fi + if which timeout 1>/dev/null; then + timeout -- $@ + else + perl -e 'BEGIN{$|=1}; $SIG{ALRM}=sub{exit 1}; alarm(shift @ARGV); system join(" ",@ARGV)' -- $@ + fi } shell_help() { - # - # Print help message and return true if args contains -?-h(elp)? - # - local msg="" - if [ "$#" -gt 0 ]; then - msg="$1" - shift - fi - - local i - #while [ $# -gt 0 ]; do - for i in $@; do - case "$i" in - -h|-help|--help) - printf "$msg\n" + # + # Print help message and return true if args contains -?-h(elp)? + # + local msg="" + if [ "$#" -gt 0 ]; then + msg="$1" + shift + fi + + local i + #while [ $# -gt 0 ]; do + for i in $@; do + case "$i" in + -h|-help|--help) + printf "$msg\n" # NB 25.02.17 case $OSTYPE in # NB 25.02.17 darwin*) printf "$msg\n" ;; # NB 25.02.17 *) echo -e "$msg" ;; # NB 25.02.17 esac - return 0 - ;; - esac - shift 2>/dev/null || break - done + return 0 + ;; + esac + shift 2>/dev/null || break + done - return 1 + return 1 } shell_help_noarg() { - # - # Wrapper for shell_help - # - local msg="" - if [ "$#" -gt 0 ]; then - msg="$1" - shift - fi + # + # Wrapper for shell_help + # + local msg="" + if [ "$#" -gt 0 ]; then + msg="$1" + shift + fi - local args="$*" - [ -z "$args" ] && args="--help" + local args="$*" + [ -z "$args" ] && args="--help" - shell_help "$msg" "$args" + shell_help "$msg" "$args" } shell_functions() { # NB 15.02.16 set | perl -ne '/^([a-z][\w_-]+) \(\)\s*$/ and print "$1\n"' - typeset -f | awk '/^[a-z]+[a-zA-Z0-9_]+ \(\)/{print $1}' + typeset -f | awk '/^[a-z]+[a-zA-Z0-9_]+ \(\)/{print $1}' # NB 07.02.17 typeset -f | perl -ne '/^([a-z][\w_-]+) \(\)\s*\{*$/ and print "$1\n"' - #set | perl -ne '/^([a-z][\w_-]+) \(\)\s*$/ and print "$1 "' | perl -pe 's/\s+$//' + #set | perl -ne '/^([a-z][\w_-]+) \(\)\s*$/ and print "$1 "' | perl -pe 's/\s+$//' } shell_ssh() { - local ssh_opt; case "$1" in -*) ssh_opt="$1"; [ "$#" -gt 0 ] && 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 - - local args - if [ -z "$@" ]; then - [ -z "$ssh_opt" ] && ssh_opt='-t' - - case "$ZSH_NAME" in - *zsh) ssh $ssh_opt "$server" "source $tmp && zsh -f -d; rm -f $tmp" ;; - *) ssh $ssh_opt "$server" "bash --rcfile $tmp; rm -f $tmp" ;; - esac - - else + local ssh_opt; case "$1" in -*) ssh_opt="$1"; [ "$#" -gt 0 ] && 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 + + local args + if [ -z "$@" ]; then + [ -z "$ssh_opt" ] && ssh_opt='-t' + + case "$ZSH_NAME" in + *zsh) ssh $ssh_opt "$server" "source $tmp && zsh -f -d; rm -f $tmp" ;; + *) ssh $ssh_opt "$server" "bash --rcfile $tmp; rm -f $tmp" ;; + esac + + else # NB 31.08.16 ssh $ssh_opt "$server" "bash && rm -f $tmp" <<< ". $tmp; $@" - echo ". $tmp; $@" | ssh $ssh_opt "$server" "bash && rm -f $tmp" + echo ". $tmp; $@" | ssh $ssh_opt "$server" "bash && rm -f $tmp" - fi + fi } ips() { - shell_help "Usage: $FUNCNAME [INTERFACE]. Print ip adresses." "$@" && return 1 - ( ip addr show || ifconfig ) 2>/dev/null | awk '/^[\t ]*inet /{sub("^[^0-9]*","",$2); sub("/[0-9]+","",$2); if ($2 != "127.0.0.1"){print $2}}' + shell_help "Usage: $FUNCNAME [INTERFACE]. Print ip adresses." "$@" && return 1 + ( ip addr show || ifconfig ) 2>/dev/null | awk '/^[\t ]*inet /{sub("^[^0-9]*","",$2); sub("/[0-9]+","",$2); if ($2 != "127.0.0.1"){print $2}}' } env_add_path() { - # Add paths to a variables - # Usage PATH=`env_add_path "$PATH" "/blbabla"` - local env_value=$1; shift - [ -z "$env_value" ] || env_value=$(printf %s "$env_value" | awk -v RS=: '{ if (!arr[$0]++) {printf("%s%s",!ln++?"":":",$0)}}') + # Add paths to a variables + # Usage PATH=`env_add_path "$PATH" "/blbabla"` + local env_value=$1; shift + [ -z "$env_value" ] || env_value=$(printf %s "$env_value" | awk -v RS=: '{ if (!arr[$0]++) {printf("%s%s",!ln++?"":":",$0)}}') - for p in $@; do - [ -e "$p" ] || continue - case "${env_value}" in - *:$p|*:$p:*|$p:*|$p) continue;; - esac - [ -z "$env_value" ] || env_value=":${env_value}" - env_value="${p}${env_value}" - done + for p in $@; do + [ -e "$p" ] || continue + case "${env_value}" in + *:$p|*:$p:*|$p:*|$p) continue;; + esac + [ -z "$env_value" ] || env_value=":${env_value}" + env_value="${p}${env_value}" + done - # Clean up doubles - [ -z "$env_value" ] || echo "$env_value" + # Clean up doubles + [ -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 + 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 - cat | perl -ne 'for (split("",$_)) { print sprintf("%3d",ord($_)).":$_\n"; }' - elif [ -n "$*" ]; then - perl -ne 'for (split("",$_)) { print sprintf("%3d",ord($_)).":$_\n"; }' $@ - else - perl -e 'binmode(STDOUT, ":utf8"); for(32..255){ print "$_:".chr($_)."\n"; }' - fi + if [ ! -t 0 ]; then + cat | perl -ne 'for (split("",$_)) { print sprintf("%3d",ord($_)).":$_\n"; }' + elif [ -n "$*" ]; then + perl -ne 'for (split("",$_)) { print sprintf("%3d",ord($_)).":$_\n"; }' $@ + else + perl -e 'binmode(STDOUT, ":utf8"); for(32..255){ print "$_:".chr($_)."\n"; }' + fi } psmem() { - local ps=`ps ax -O rss 2>/dev/null` - echo "$ps"| awk "\ + local ps=`ps ax -O rss 2>/dev/null` + echo "$ps"| awk "\ BEGIN { count = 0; sum= 0; } /^ *[0-9]+/ && /$1/ { count ++; sum += \$2 } END { - print \"Number of processes =\",count; - if (count < 1) { count = 1 } - print \"Memory usage per process =\",sum/1024/count, \"MB\"; - print \"Total memory usage =\", sum/1024, \"MB\"; + print \"Number of processes =\",count; + if (count < 1) { count = 1 } + print \"Memory usage per process =\",sum/1024/count, \"MB\"; + print \"Total memory usage =\", sum/1024, \"MB\"; } " - return + return } unzipurl() { - local url file - url=$1 - file=`echo "$url"|awk -F/ '/\.zip$/ { print $NF }'` - [ -z "$file" ] && file="$FUNCNAME.zip" - wget "$url" -O "$file" && unzip "$file" - rm "$file" + local url file + url=$1 + file=`echo "$url"|awk -F/ '/\.zip$/ { print $NF }'` + [ -z "$file" ] && file="$FUNCNAME.zip" + wget "$url" -O "$file" && unzip "$file" + rm "$file" } txt2ascii() { - in="cat" - [ -z "$@" ] || in="echo \"$@\"" - $in | perl -e ' + in="cat" + [ -z "$@" ] || in="echo \"$@\"" + $in | perl -e ' while (<>) { - for (split("",$_)) { - print sprintf("\%3d",ord($_)).":$_\n"; - } + for (split("",$_)) { + print sprintf("\%3d",ord($_)).":$_\n"; + } } ' } secs2h () { - local T=$1 - local D=$((T/60/60/24)) - local H=$((T/60/60%24)) - local M=$((T/60%60)) - local S=$((T%60)) + local T=$1 + local D=$((T/60/60/24)) + local H=$((T/60/60%24)) + local M=$((T/60%60)) + local S=$((T%60)) - if [[ ${D} != 0 ]] - then - printf '%d days %02d:%02d:%02d\n' $D $H $M $S - else - printf '%02d:%02d:%02d\n' $H $M $S - fi + if [[ ${D} != 0 ]] + then + printf '%d days %02d:%02d:%02d\n' $D $H $M $S + else + printf '%02d:%02d:%02d\n' $H $M $S + fi } bytes2h() { - local in args - - if [ "$1" = "-regex" ]; then - args="regex=$2" - shift - shift - fi - in="cat" - [ -z "$@" ] || in="echo $@" - - #use Number::Bytes::Human qw/&format_bytes/; - #s/$exp/bytes2h($1)/ge; - $in | perl -pe ' + local in args + + if [ "$1" = "-regex" ]; then + args="regex=$2" + shift + shift + fi + in="cat" + [ -z "$@" ] || in="echo $@" + + #use Number::Bytes::Human qw/&format_bytes/; + #s/$exp/bytes2h($1)/ge; + $in | perl -pe ' BEGIN{ - use NB::Functions qw/bytes2h/; - $exp = "([\\d\\.]+)"; - if (@ARGV and $ARGV[0] =~ /regex=(\S+)/) { - shift @ARGV; - $exp = $1; - } + use NB::Functions qw/bytes2h/; + $exp = "([\\d\\.]+)"; + if (@ARGV and $ARGV[0] =~ /regex=(\S+)/) { + shift @ARGV; + $exp = $1; + } }; s/$exp/bytes2h($1)/ge;' $args } yaml2perl() { - perl -MYAML -MData::Dumper -e 'print Dumper YAML::Load(join("",<>))' $@ + perl -MYAML -MData::Dumper -e 'print Dumper YAML::Load(join("",<>))' $@ } json2perl() { - perl -MJSON -MData::Dumper -e 'print Dumper JSON::decode_json(join("",<>))' $@ + perl -MJSON -MData::Dumper -e 'print Dumper JSON::decode_json(join("",<>))' $@ } hl(){ - #cat | grep --color=always -E "(^|($@))" - perl -MTerm::ANSIColor -pe '$e=qq@'$@'@;$e=~s,/,\\/,g; s/($e)/color("red").$1.color("reset")/ge' + #cat | grep --color=always -E "(^|($@))" + perl -MTerm::ANSIColor -pe '$e=qq@'$@'@;$e=~s,/,\\/,g; s/($e)/color("red").$1.color("reset")/ge' } ls_tree() { - tree -tDphug + tree -tDphug } replace_tag_content() { - local tag; tag="$1"; shift - local content; content="$@" - perl -ne 'BEGIN { + local tag; tag="$1"; shift + local content; content="$@" + perl -ne 'BEGIN { $tag = shift(@ARGV)." - replace_tag_content"; $t1 = "# >>> $tag"; $t2 = "# <<< $tag"; @@ -352,9 +352,9 @@ $content = "$t1\n$content\n$t2\n" } if (/^$t1$/ .. /^$t2$/) { - print "$content" and $content="" if /^$t1$/; + print "$content" and $content="" if /^$t1$/; } else { - print "$_"; + print "$_"; } END { print "$content" if $content; } @@ -363,156 +363,156 @@ 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 - local exp num - num=0; case "$1" in [0-9]*) num=$1; shift ;; esac - exp=$1; shift + [ -z "$*" -o "$1" == "-h" -o "$1" == "--help" ] && echo "Usage: $FUNCNAME [capture number|default 0] PERL_REGEXP FILES..." && return + local exp num + num=0; case "$1" in [0-9]*) num=$1; shift ;; esac + exp=$1; shift - if [ -z "$_getexp_pcregrep" ]; then - _getexp_pcregrep=0 - which pcregrep > /dev/null && _getexp_pcregrep=1 - fi + if [ -z "$_getexp_pcregrep" ]; then + _getexp_pcregrep=0 + which pcregrep > /dev/null && _getexp_pcregrep=1 + fi - if [ $_getexp_pcregrep = 1 ]; then - pcregrep -o${num} "$exp" $@ - else - perl -ne 'BEGIN{$e=shift @ARGV; $n=shift(@ARGV) or 0; $e="($e)" and $n=1 if $n==0} (@_=m{$e}) and ($n<=scalar(@_)) and print ($n==0 ? $_[0] : $_[$n-1].chr(10))' "$exp" "$num" $@ - fi - return + if [ $_getexp_pcregrep = 1 ]; then + pcregrep -o${num} "$exp" $@ + else + perl -ne 'BEGIN{$e=shift @ARGV; $n=shift(@ARGV) or 0; $e="($e)" and $n=1 if $n==0} (@_=m{$e}) and ($n<=scalar(@_)) and print ($n==0 ? $_[0] : $_[$n-1].chr(10))' "$exp" "$num" $@ + fi + return } getexp_url() { curl -ks "$2" | perl -ne 'm,('$1'), and print $1 and exit'; } ls_img_black() { - local threshold; threshold=10 - local ret; ret=1 + local threshold; threshold=10 + local ret; ret=1 - shell_help_noarg "Usage: $FUNCNAME [-threshold PCENT (default: $threshold)] IMG1 IMG2 ..." "$@" && return 1 + shell_help_noarg "Usage: $FUNCNAME [-threshold PCENT (default: $threshold)] IMG1 IMG2 ..." "$@" && return 1 - while [ $# -gt 0 ]; do + while [ $# -gt 0 ]; do - if [ "$1" = "-threshold" ]; then - threshold=$2; shift 2; continue - fi + if [ "$1" = "-threshold" ]; then + threshold=$2; shift 2; continue + fi - #colors=$(convert $1 -format "%[mean]" info:-) + #colors=$(convert $1 -format "%[mean]" info:-) - if ! convert "$1" -threshold ${threshold}% -format %c histogram:info:- | grep -qm1 white; then - echo "$1" - ret=0 - fi - shift + if ! convert "$1" -threshold ${threshold}% -format %c histogram:info:- | grep -qm1 white; then + echo "$1" + ret=0 + fi + shift - done + done - return $ret + return $ret } img2pdf() { - convert -compress zip -colors 256 -colorspace GRAY -resize 1020x1320 -density 120x120 -units PixelsPerInch $@ + convert -compress zip -colors 256 -colorspace GRAY -resize 1020x1320 -density 120x120 -units PixelsPerInch $@ } jpg2avi() { - shell_help_noarg "Usage: $FUNCNAME [-h] JPG_FILES AVI_FILE" "$@" && return - local still; still=/tmp/$FUNCNAME.$$.txt; [ -x /dev/shm ] && tmp=/dev/shm/$FUNCNAME.$$.txt + shell_help_noarg "Usage: $FUNCNAME [-h] JPG_FILES AVI_FILE" "$@" && return + local still; still=/tmp/$FUNCNAME.$$.txt; [ -x /dev/shm ] && tmp=/dev/shm/$FUNCNAME.$$.txt - local avi jpg + local avi jpg - local tot; tot=$# - local count; count=0 + local tot; tot=$# + local count; count=0 - while [ $# -gt 0 ]; do - count=$(($count+1)) + while [ $# -gt 0 ]; do + count=$(($count+1)) - if [ "$count" = "$tot" ]; then - avi="$1"; shift; break - fi - echo "$1" >> "$still" + if [ "$count" = "$tot" ]; then + avi="$1"; shift; break + fi + echo "$1" >> "$still" - shift - done + shift + done - #echo $(cat $still)" -> $avi" - 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" + #echo $(cat $still)" -> $avi" + 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() { - 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 - #shell_help "$usage" "$@" && return 1 - [ -z "$*" ] && echo "$usage" && return 1 - if ! perl -e '' 2>/dev/null; then - echo "cron_d_install: please install perl" 1>&2 - return 1 - fi - - if [ "$#" -gt 0 ]; then - case "$1" in -t|--test) test=1; shift ;; esac - fi - - #echo "$#"; return - if [ $# -gt 3 ]; then - shell_help_noarg "$usage" "--help" && return 1 - - #elif [ $# -eq 3 ]; then - elif [ ! -e "$1" ]; then - fname=$1; shift - tmp="/tmp/$FUNCNAME.$$" - trap "rm -f $tmp*" EXIT - file="$tmp.cron" + 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 + #shell_help "$usage" "$@" && return 1 + [ -z "$*" ] && echo "$usage" && return 1 + if ! perl -e '' 2>/dev/null; then + echo "cron_d_install: please install perl" 1>&2 + return 1 + fi + + if [ "$#" -gt 0 ]; then + case "$1" in -t|--test) test=1; shift ;; esac + fi + + #echo "$#"; return + if [ $# -gt 3 ]; then + shell_help_noarg "$usage" "--help" && return 1 + + #elif [ $# -eq 3 ]; then + elif [ ! -e "$1" ]; then + fname=$1; shift + tmp="/tmp/$FUNCNAME.$$" + trap "rm -f $tmp*" EXIT + file="$tmp.cron" # NB 31.08.16 cat <<< "$1" > "$file"; shift - echo "$1" > "$file"; shift - exp=$1; shift - - else - file=$1; shift - exp=$1; shift - fname=$(basename "$file") - - fi - - # - # Case 1 - /etc/cron.d - # - if [ -d /etc/cron.d ]; then - - if [ "$test" = "1" ]; then - printf '%s' "$FUNCNAME: /etc/cron.d: $fname" - [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && echo ': Changed' && return - echo ': Not changed' - return 1 - fi - - [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && cp "$file" "/etc/cron.d/$fname" - return - fi - - # - # Case 2 - crontab - # - - cron=$( - perl -e ' + echo "$1" > "$file"; shift + exp=$1; shift + + else + file=$1; shift + exp=$1; shift + fname=$(basename "$file") + + fi + + # + # Case 1 - /etc/cron.d + # + if [ -d /etc/cron.d ]; then + + if [ "$test" = "1" ]; then + printf '%s' "$FUNCNAME: /etc/cron.d: $fname" + [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && echo ': Changed' && return + echo ': Not changed' + return 1 + fi + + [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && cp "$file" "/etc/cron.d/$fname" + return + fi + + # + # Case 2 - crontab + # + + cron=$( + perl -e ' $diff = 0; $e = shift @ARGV; @f = map {chomp; $_} grep {!/^(#|\s*$)/} <>; for ($i=0;$i<@f;$i++) { - @F = split(" ",$f[$i]); - next if $f[$i] !~ /^[\*\d]/ or @F<6; - if ($F[5] ne "root"){ - $_ = join(" ",@F[6..$#F]); - s/"/\\"/g; - @_=qq|su -l "$F[5]" -c "$_"|; - @F = (@F[0..4],"",@_); - }; - $f[$i] = join(" ",@F[0..4,6..$#F]); + @F = split(" ",$f[$i]); + next if $f[$i] !~ /^[\*\d]/ or @F<6; + if ($F[5] ne "root"){ + $_ = join(" ",@F[6..$#F]); + s/"/\\"/g; + @_=qq|su -l "$F[5]" -c "$_"|; + @F = (@F[0..4],"",@_); + }; + $f[$i] = join(" ",@F[0..4,6..$#F]); } %f = map {$_=>1} @f; @@ -521,190 +521,190 @@ for ($i=0;$i<@f;$i++) { %c = map {$_=>1} @c; for (@c) { - 0 and print STDERR "-$_\n" if $e and !$f{$_} and /$e/; - $diff=1 and next if $e and !$f{$_} and /$e/; - print "$_\n"; + 0 and print STDERR "-$_\n" if $e and !$f{$_} and /$e/; + $diff=1 and next if $e and !$f{$_} and /$e/; + print "$_\n"; } for (@f) { - $diff=2 and print "$_\n" if !$c{$_}; + $diff=2 and print "$_\n" if !$c{$_}; } exit $diff ? 0 : 1; ' "$exp" "$file" ) - if [ "$test" = "1" ]; then - printf '%s' "$FUNCNAME: crontab: $fname" - #echo -e "\n<\n$(crontab -l)\n>\n<\n$cron\n>\n" - [ "$cron" != "$(crontab -l)" ] && echo ': Changed' && return - echo ": Not changed; return 1" - fi + if [ "$test" = "1" ]; then + printf '%s' "$FUNCNAME: crontab: $fname" + #echo -e "\n<\n$(crontab -l)\n>\n<\n$cron\n>\n" + [ "$cron" != "$(crontab -l)" ] && echo ': Changed' && return + echo ": Not changed; return 1" + fi # NB 31.08.16 [ "$cron" != "$(crontab -l)" ] && crontab <<< "$cron" - [ "$cron" != "$(crontab -l)" ] && echo "$cron" | crontab + [ "$cron" != "$(crontab -l)" ] && echo "$cron" | crontab } csv2human() { - shell_help "Usage: $FUNCNAME [PERL_REGEXP --sep|-s (default: tab)] [--noheader|-nh]" "$@" && return; - local sep noheader; - sep='\t'; - noheader=0; - local files='' - - while [ $# -gt 0 ]; do - - if [ -e "$1" ]; then - files="$files $1" - shift - continue - fi - - case "$1" in - --sep|-s) - sep=$2; - shift - ;; - --noheader|-nh) - noheader=1 - ;; - *) - echo "Unknow option: $1 at $0!"; - return 1 - ;; - - esac - - shift - - done - - [ -z "$@" ] || sep="$@"; - - cat $files | perl -MEncode -F"$sep" -ane 'BEGIN { - $noheader = shift @ARGV; - @len = (); @lines = (); - binmode( STDOUT, "utf8:" ); + shell_help "Usage: $FUNCNAME [PERL_REGEXP --sep|-s (default: tab)] [--noheader|-nh]" "$@" && return; + local sep noheader; + sep='\t'; + noheader=0; + local files='' + + while [ $# -gt 0 ]; do + + if [ -e "$1" ]; then + files="$files $1" + shift + continue + fi + + case "$1" in + --sep|-s) + sep=$2; + shift + ;; + --noheader|-nh) + noheader=1 + ;; + *) + echo "Unknow option: $1 at $0!"; + return 1 + ;; + + esac + + shift + + done + + [ -z "$@" ] || sep="$@"; + + cat $files | perl -MEncode -F"$sep" -ane 'BEGIN { + $noheader = shift @ARGV; + @len = (); @lines = (); + binmode( STDOUT, "utf8:" ); }; for ($i=0;$i<@F;$i++) { - chomp($F[$i]); - $F[$i] =~ s/^\r//; - $F[$i] = Encode::decode_utf8($F[$i]); - $len[$i] = $l if ( $l = length($F[$i])) >= $len[$i]; + chomp($F[$i]); + $F[$i] =~ s/^\r//; + $F[$i] = Encode::decode_utf8($F[$i]); + $len[$i] = $l if ( $l = length($F[$i])) >= $len[$i]; } push @lines, [@F]; END { - exit unless @lines; - $i = 0; - $t = -1; - $tot = @lines - ($noheader ? 0 : 1); - $format = "| ".join(" | ",map {$t+=$_+3; "\%-".$_."s"} @len)." |".chr(10); - $sep_line = "+".join("+",map {("-"x($_+2))} @len)."+".chr(10); - print $sep_line; - while ($_ = shift @lines) { - printf $format,@$_; - print $sep_line if !$noheader and !$i++; - } - print $sep_line; - print "$tot Records\n"; + exit unless @lines; + $i = 0; + $t = -1; + $tot = @lines - ($noheader ? 0 : 1); + $format = "| ".join(" | ",map {$t+=$_+3; "\%-".$_."s"} @len)." |".chr(10); + $sep_line = "+".join("+",map {("-"x($_+2))} @len)."+".chr(10); + print $sep_line; + while ($_ = shift @lines) { + printf $format,@$_; + print $sep_line if !$noheader and !$i++; + } + print $sep_line; + print "$tot Records\n"; } ' $noheader } mail_relay_closed() { - shell_help "Usage: $FUNCNAME MAIL_SERVER" "$@" && return; - local out=$( - cat < /dev/null| strings -a` + local mbr=`dd if=/dev/sda bs=446 count=1 2> /dev/null| strings -a` - case "$mbr" in - *GRUB*) echo "GRUB" ;; - *LILO*) echo "LILO" ;; - *) return 1 ;; - esac + case "$mbr" in + *GRUB*) echo "GRUB" ;; + *LILO*) echo "LILO" ;; + *) return 1 ;; + esac - return 0 + return 0 } is_empty() { [[ -z $(ls -A "/$1" | head -1) ]]; } is_prompt() { - #[[ $- == *i* ]] && echo "Interactive" || echo "Not interactive" - case "$-" in - *i*) return 0 ;; - *) return 1 ;; - esac + #[[ $- == *i* ]] && echo "Interactive" || echo "Not interactive" + case "$-" in + *i*) return 0 ;; + *) return 1 ;; + esac } url2nc() { - local in="cat"; - [ -n "$*" ] && in="echo $*"; - eval $in | perl -ne ' BEGIN{%p=("ssh"=>22,"https"=>443,"http"=>80);}; /([\w\._-]+):(\d+)\b/ and print "$1 $2\n" and next; m,^\w+://, or $_="ssh://$_"; m,^(\w+)://(?:[^@]+@)?([\w\._-]+), and print "$2 ".($p{$1}||22)."\n" and next' + local in="cat"; + [ -n "$*" ] && in="echo $*"; + eval $in | perl -ne ' BEGIN{%p=("ssh"=>22,"https"=>443,"http"=>80);}; /([\w\._-]+):(\d+)\b/ and print "$1 $2\n" and next; m,^\w+://, or $_="ssh://$_"; m,^(\w+)://(?:[^@]+@)?([\w\._-]+), and print "$2 ".($p{$1}||22)."\n" and next' } ls_users() { - case "$OSTYPE" in - darwin*) - dscacheutil -q user \ - | perl -ne '/^\w+:\s+(.*)?$/; push @_,$1; print join(":",$_[0],"*",@_[2,3,6,4,5])."\n" and @_=() if /^\s*$/' \ - | sort -u \ - | sort -t : -k3,4 -n \ - ; - ;; - *) - if [ -n "$(which getent 2>/dev/null)" ]; then - getent passwd - elif [ -e /etc/passwd ]; then - grep '^[^#:]\+:' /etc/passwd | sort -u | sort -t : -k3,4 -n - else - echo "$FUNCNAME: can not find any users on this system: #OSTYPE" 1>&2 - return 1 - fi - ;; - esac + case "$OSTYPE" in + darwin*) + dscacheutil -q user \ + | perl -ne '/^\w+:\s+(.*)?$/; push @_,$1; print join(":",$_[0],"*",@_[2,3,6,4,5])."\n" and @_=() if /^\s*$/' \ + | sort -u \ + | sort -t : -k3,4 -n \ + ; + ;; + *) + if [ -n "$(which getent 2>/dev/null)" ]; then + getent passwd + elif [ -e /etc/passwd ]; then + grep '^[^#:]\+:' /etc/passwd | sort -u | sort -t : -k3,4 -n + else + echo "$FUNCNAME: can not find any users on this system: #OSTYPE" 1>&2 + return 1 + fi + ;; + esac } reverse() { perl -e 'print reverse <>'; } @@ -712,31 +712,31 @@ epoch2date() { perl -M'POSIX strftime' -e 'e2d($ARGV[0]) and print "\n" and exit #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() { - ( - local find_args="$@" - [ -z "$find_args" ] && args="." + ( + local find_args="$@" + [ -z "$find_args" ] && args="." - case "$OSTYPE" in - darwin*) - find $find_args -type f -exec stat -f '%m'$'\t''%N' {} \; - ;; - *) - find $@ -type f -printf "%T@\t%p\n" | sed 's/^\([0-9]\+\)\.[0-9]\+/\1/' - ;; - esac + case "$OSTYPE" in + darwin*) + find $find_args -type f -exec stat -f '%m'$'\t''%N' {} \; + ;; + *) + find $@ -type f -printf "%T@\t%p\n" | sed 's/^\([0-9]\+\)\.[0-9]\+/\1/' + ;; + esac - ) | sort -k1 -n $sort_args | epoch2date - # strftime "%a %b %e %H:%M:%S %Y" + ) | sort -k1 -n $sort_args | epoch2date + # strftime "%a %b %e %H:%M:%S %Y" } shell_functions2sql() { - local t="$1"; shift - local f - ( - for f in `shell_functions`; do - type "$f" || continue - done - ) | perl -MData::Dumper -e ' + local t="$1"; shift + local f + ( + for f in `shell_functions`; do + type "$f" || continue + done + ) | perl -MData::Dumper -e ' BEGIN{$t=shift @ARGV and print qq|PRAGMA encoding="UTF-8";\n| ."DROP TABLE IF EXISTS $t;\n" ."CREATE TABLE $t (name VARCHAR(255) PRIMARY KEY,code BLOB);\n" @@ -748,32 +748,32 @@ $_=join("",<>);@_=(map{s/^\s*(.*?)\s*$/$1/m;$_}split(/^(.*?) is a function$/m,$_ 0&&die Dumper($_[2]); $q=chr(39); while (@_) { - $k=shift @_; - next unless $k; - $v=shift @_; - $v =~ s/$q/$q.$q/ge; - print $t.qq|VALUES ($q$k$q,$q$v$q);\n|; + $k=shift @_; + next unless $k; + $v=shift @_; + $v =~ s/$q/$q.$q/ge; + print $t.qq|VALUES ($q$k$q,$q$v$q);\n|; } ' "$t" } shell_replace() { - local usage="Usage: shell_replace [-i[.BACKUP_EXTENSION]] [-find PAHT]" - if [ -z "$*" -o "$1" = "--help" -o "$1" = "-h" ]; then - echo "$usage" - return - fi + local usage="Usage: shell_replace [-i[.BACKUP_EXTENSION]] [-find PAHT]" + if [ -z "$*" -o "$1" = "--help" -o "$1" = "-h" ]; then + echo "$usage" + return + fi - if [ "$1" = "-find" ]; then - shift - grep -ErlI '^.{1,3}SHELL_REPLACE (.*)$/ .. /^\s*.{1,3}SHELL_REPLACE (.*)$/ .. /^\s*.{1,3}/dev/null; then - curl -sk $* + if which curl 1>/dev/null; then + curl -sk $* - elif which wget 1>/dev/null; then - wget -O - --quiet --no-verbose --no-check-certificate --timeout 5 --tries 1 $* + elif which wget 1>/dev/null; then + wget -O - --quiet --no-verbose --no-check-certificate --timeout 5 --tries 1 $* - elif which lynx 1>/dev/null; then - lynx --source -dump $* + elif which lynx 1>/dev/null; then + lynx --source -dump $* - else - echo "Can't find web client" 1>&2 - fi + else + echo "Can't find web client" 1>&2 + fi } # NB 07.01.17 alias Get=http_get http_head() { - if which curl 1>/dev/null; then - curl -sk --head $* + if which curl 1>/dev/null; then + curl -sk --head $* - elif which wget 1>/dev/null; then - http_get --save-headers - $* | perl -ne '/^\s*$/ and last;print' + elif which wget 1>/dev/null; then + http_get --save-headers - $* | perl -ne '/^\s*$/ and last;print' - elif which lynx 1>/dev/null; then - lynx --head -dump $* + elif which lynx 1>/dev/null; then + lynx --head -dump $* - else - echo "Can't find web client" 1>&2 - fi + else + echo "Can't find web client" 1>&2 + fi } password_create() { - tr -cd '[:alnum:]' < /dev/urandom | head -c${1:-30} - echo + tr -cd '[:alnum:]' < /dev/urandom | head -c${1:-30} + echo } #unset _which_cache _which_cache="" which_cache() { - #/usr/bin/which - local IFS=':' - local p - - # Cache - case "$_which_cache|" in - *"|$1=1|"*) - nb_debug "which_cache() $1=1" - return 0 - ;; - *"|$1=0|"*) - nb_debug "which_cache() $1=0" - return 1 - ;; - esac - #nb_debug "which_cache() $1=?" - - for p in $PATH; do - test -x "$p/$1" || continue - - _which_cache="$_which_cache|$1=1" - echo "$1" - return 0 - done - - _which_cache="$_which_cache|$1=0" - return 1 + #/usr/bin/which + local IFS=':' + local p + + # Cache + case "$_which_cache|" in + *"|$1=1|"*) + nb_debug "which_cache() $1=1" + return 0 + ;; + *"|$1=0|"*) + nb_debug "which_cache() $1=0" + return 1 + ;; + esac + #nb_debug "which_cache() $1=?" + + for p in $PATH; do + test -x "$p/$1" || continue + + _which_cache="$_which_cache|$1=1" + echo "$1" + return 0 + done + + _which_cache="$_which_cache|$1=0" + return 1 } uncomment() { - grep -Ev '^\s*(["/\#\;-]|$)' $@ | grep -Ev "^\s*'" | cat + grep -Ev '^\s*(["/\#\;-]|$)' $@ | grep -Ev "^\s*'" | cat } gid_from_to() { - local cmd="find / -uid $1 -exec chgrp -v $2 {} \;" - echo "Press a key to run:" - echo "$cmd" - read - eval "$cmd" + local cmd="find / -uid $1 -exec chgrp -v $2 {} \;" + echo "Press a key to run:" + echo "$cmd" + read + eval "$cmd" } uid_from_to() { - local cmd="find / -uid $1 -exec chown -v $2 {} \;" - echo "Press a key to run:" - echo "$cmd" - read - eval "$cmd" + local cmd="find / -uid $1 -exec chown -v $2 {} \;" + echo "Press a key to run:" + echo "$cmd" + read + eval "$cmd" } lscolors() { - local color=16 + local color=16 - while [ $color -lt 245 ]; do - echo -ne "$color: \\033[38;5;${color}mhello\\033[48;5;${color}mworld\\033[0m" - printf '\t%s\n' "\\033[38;5;${color}mhello\\033[48;5;${color}mworld\\033[0m" - color=$(($color+1)) - done + while [ $color -lt 245 ]; do + echo -ne "$color: \\033[38;5;${color}mhello\\033[48;5;${color}mworld\\033[0m" + printf '\t%s\n' "\\033[38;5;${color}mhello\\033[48;5;${color}mworld\\033[0m" + color=$(($color+1)) + done } tab2space() { @@ -910,5 +910,7 @@ space2tab() { local size=2 [ "$1" = "-size" ] && shift && size=$1 && shift local space=$(eval "printf ' %.0s' {1..$size}") + #perl -pe "1 while s/^(\t*)?$space/$1\t/" $@ + #perl -pe "1 while s/^$space/\t/; 1 while s/(\t)+$space/$1\t/" $@ perl -pe "1 while s/^$space/\t/" $@ } diff --git a/etc/vim/source/functions.vim b/etc/vim/source/functions.vim index e6990922..bdb8b19c 100644 --- a/etc/vim/source/functions.vim +++ b/etc/vim/source/functions.vim @@ -30,14 +30,6 @@ func! FileInit(...) let bashenv = $BASH_ENV let $BASH_ENV = "" - let vimplate = $VIMHOME."/vimplate-dump ".$VIMPLATERC - let tmp = '/tmp/vimplate-dump.tmp' - - call system(vimplate ." > ". tmp) - - exec "source ".tmp - - call delete(tmp) let g:File['path'] = expand('%:p') let g:File['ext'] = expand('%:e') diff --git a/etc/vim/vimplate-dump b/etc/vim/vimplate-dump deleted file mode 100755 index 6e84c104..00000000 --- a/etc/vim/vimplate-dump +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env perl -require ( @ARGV ? $ARGV[0] : $ENV{HOME}.'/.vimplaterc' ); - - -print map { - $v=$Config::user->{nico}{$_}; - $v=~ s/\n/\\n/g; - $v=~ s/(')/\\$1/g; - - "let g:File['$_'] = ".qq|'$v'\n|; - -} keys %{ $Config::user->{nico} } -- 2.47.3