From 622a72f5aa493d025b6357686dbdf4315f6078a2 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Wed, 20 Mar 2019 04:36:01 +0000 Subject: [PATCH] bin/nb-install --- bin/cron_d2crontab | 2 +- bin/nb-install | 2 - etc/profile | 2 +- etc/profile.d/functions | 141 +++++++++++++++++++++------------------- 4 files changed, 76 insertions(+), 71 deletions(-) diff --git a/bin/cron_d2crontab b/bin/cron_d2crontab index 85590f10..9f826532 100755 --- a/bin/cron_d2crontab +++ b/bin/cron_d2crontab @@ -195,7 +195,7 @@ Quick usage! =head1 DESCRIPTION -Take a cron.d/ file and merge it with the current user crontab on stdout +Take a file in cron.d format and merge it with the current user crontab on stdout =head1 OPTIONS diff --git a/bin/nb-install b/bin/nb-install index 80beed0f..2d531329 100755 --- a/bin/nb-install +++ b/bin/nb-install @@ -64,9 +64,7 @@ main() { elif [ -e /etc/nb/cron.no ]; then rm -f /etc/cron.d/nb /var/log/nb.log else - #shell_help_noarg "zaza" "$@" && exit 0 cron_d_install "$NB_ROOT/etc/cron/nb" "nb-update|$NB_ROOT|NB_ROOT|NB_LOG" && verbose "Cron: $NB_ROOT/etc/cron/nb" - #cron_d_install "test" "" "nico.*true" && verbose "Cron: nico" fi # diff --git a/etc/profile b/etc/profile index c4f7e728..876ee9e6 100755 --- a/etc/profile +++ b/etc/profile @@ -44,7 +44,7 @@ if [ -n "$NB_LOOP" ]; then fi nb_debug "ENVS: BASH_SOURCE=$BASH_SOURCE \$0=$0" -#trap 'unset NB_LOOP' EXIT +#trap 'unset NB_LOOP' RETURN export NB_LOOP=1 # diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 0627aa8a..3ef254b4 100755 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -287,12 +287,18 @@ END { } 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:?Usage: unzipurl URL} + +# NB 20.03.19 file=`echo "$url"|awk -F/ '/\.zip$/ { print $NF }'` +# NB 20.03.19 [ -z "$file" ] && file="unzipurl.zip" + + file="/tmp/unzipurl.$$.zip" + + wget "$url" -O "$file" && unzip -v "$file" + + rm "$file" } txt2ascii() { @@ -308,18 +314,18 @@ while (<>) { } 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)) - - if [[ ${D} != 0 ]] - then + 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 + else printf '%02d:%02d:%02d\n' $H $M $S - fi + fi } bytes2h() { @@ -819,12 +825,12 @@ xml2csv() { 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]" + usage="Usage: cron_d_install [-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 "$FUNCNAME: please install perl" 1>&2 + echo "cron_d_install: please install perl" 1>&2 return 1 fi @@ -833,18 +839,19 @@ cron_d_install() { fi #echo "$#"; return - if [ $# -gt 4 -o $# = 0 ]; then +# NB 20.03.19 if [ $# -gt 4 -o $# = 0 ]; then + if [ $# -gt 3 -o $# = 0 ]; 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 +# NB 20.03.19 #elif [ $# -eq 3 ]; then +# NB 20.03.19 elif [ ! -e "$1" ]; then +# NB 20.03.19 fname=$1; shift +# NB 20.03.19 tmp="/tmp/cron_d_install.$$" +# NB 20.03.19 trap "rm -f $tmp*" RETURN +# NB 20.03.19 file="$tmp.cron" + cat <<< "$1" > "$file"; shift +# NB 20.03.19 echo "$1" > "$file"; shift +# NB 20.03.19 exp=$1; shift else file=$1; shift @@ -859,7 +866,7 @@ cron_d_install() { if [ -d /etc/cron.d ]; then if [ "$test" = "1" ]; then - printf '%s' "$FUNCNAME: /etc/cron.d: $fname" + printf '%s' "cron_d_install: /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 @@ -874,47 +881,47 @@ cron_d_install() { # #@f = map {chomp; $_} grep {!/^(@|#|\s*$)/} <>; - cron=$(perl -e ' -$diff = 0; -$e = shift @ARGV; - -@f = map {chomp; $_} grep {/^(\w+=|\*|\d+)/} <>; -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]); -} -0 and warn join("\n",@f); -%f = map {$_=>1} @f; - -@ARGV = ("crontab -l|"); -@c = map {chomp; $_} <>; -%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"; -} - -for (@f) { - $diff=2 and print "$_\n" if !$c{$_}; -} - -exit $diff ? 0 : 1; -' "$exp" "$file" -) +# NB 20.03.19 cron=$(perl -e ' +# NB 20.03.19 $diff = 0; +# NB 20.03.19 $e = shift @ARGV; +# NB 20.03.19 +# NB 20.03.19 @f = map {chomp; $_} grep {/^(\w+=|\*|\d+)/} <>; +# NB 20.03.19 for ($i=0;$i<@f;$i++) { +# NB 20.03.19 @F = split(" ",$f[$i]); +# NB 20.03.19 +# NB 20.03.19 next if $f[$i] !~ /^[\*\d]/ or @F<6; +# NB 20.03.19 if ($F[5] ne "root"){ +# NB 20.03.19 $_ = join(" ",@F[6..$#F]); +# NB 20.03.19 s/"/\\"/g; +# NB 20.03.19 @_=qq|su -l "$F[5]" -c "$_"|; +# NB 20.03.19 @F = (@F[0..4],"",@_); +# NB 20.03.19 }; +# NB 20.03.19 $f[$i] = join(" ",@F[0..4,6..$#F]); +# NB 20.03.19 } +# NB 20.03.19 0 and warn join("\n",@f); +# NB 20.03.19 %f = map {$_=>1} @f; +# NB 20.03.19 +# NB 20.03.19 @ARGV = ("crontab -l|"); +# NB 20.03.19 @c = map {chomp; $_} <>; +# NB 20.03.19 %c = map {$_=>1} @c; +# NB 20.03.19 +# NB 20.03.19 for (@c) { +# NB 20.03.19 0 and print STDERR "-$_\n" if $e and !$f{$_} and /$e/; +# NB 20.03.19 $diff=1 and next if $e and !$f{$_} and /$e/; +# NB 20.03.19 print "$_\n"; +# NB 20.03.19 } +# NB 20.03.19 +# NB 20.03.19 for (@f) { +# NB 20.03.19 $diff=2 and print "$_\n" if !$c{$_}; +# NB 20.03.19 } +# NB 20.03.19 +# NB 20.03.19 exit $diff ? 0 : 1; +# NB 20.03.19 ' "$exp" "$file" +# NB 20.03.19 ) cron=$(cron_d2crontab "$exp" "$file") if [ "$test" = "1" ]; then - printf '%s' "$FUNCNAME: crontab: $fname" + printf '%s' "cron_d_install: 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" -- 2.47.3