From 5542a00248f4f925860839c0b17cf659c817148c Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Tue, 18 Nov 2014 23:03:37 +0000 Subject: [PATCH] cron improve --- bin/nb-install | 54 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/bin/nb-install b/bin/nb-install index 5676efeb..e5282464 100755 --- a/bin/nb-install +++ b/bin/nb-install @@ -1,13 +1,24 @@ #!/usr/bin/env bash +############################################################################## +# +# Install - NB 18.11.14 +# +############################################################################## . "${BASH_SOURCE%/*}/../etc/profile.sh" || exit declare -r NAME="$(basename "${0}")" - +declare TMP=/tmp/$NAME.$$ declare install="rsync -au --force" +# +# Args +# case "$@" in *-h*|*-help*) exec echo "Usage: $NAME [-v] [-n]";; esac case "$@" in *-v*) install="$install -v";; esac case "$@" in *-n*) install="$install -n";; esac +# +# Functions +# function verbose() { printf '> %s\n' "$@" } @@ -16,41 +27,58 @@ function crond2tab() { local src="$1" } +function fdiff() { + [ -n "$(diff -q $1 $2)" ] && return 0 + return 1 +} + # # Cron # if [ -d /etc/cron.d ]; then - if [ -n "$(diff -q $NB_ROOT/etc/cron/nb /etc/cron.d/nb)" ]; then + if fdiff "$NB_ROOT/etc/cron/nb" "/etc/cron.d/nb"; then verbose "Install /etc/cron.d/nb" $install "$NB_ROOT/etc/cron/nb" /etc/cron.d/nb fi else - crontab -l > /tmp/nb-crontab.orig + crontab -l > $TMP.crontab.orig.orig ( crontab -l | grep -vE 'nb-update|^PATH' # Delete user perl -ane '/^PATH/ and print; /^[\*\d]/ or next; print join(" ",@F[0..4,6..$#F])."\n"' $NB_ROOT/etc/cron/nb - ) > /tmp/nb-crontab.new + ) > $TMP.crontab.orig.new - if [ -n "$(diff -q /tmp/nb-crontab.orig /tmp/nb-crontab.new)" ]; then + if fdiff $TMP.crontab.orig.orig $TMP.crontab.orig.new; then verbose "Install /etc/cron.d/nb" - crontab /tmp/nb-crontab.new + crontab $TMP.crontab.orig.new fi - rm -f /tmp/nb-crontab.* fi # # Profile # +echo "[ -r $NB_ROOT/etc/profile.sh ] && . $NB_ROOT/etc/profile.sh" > $TMP.profile if [ -d /etc/profile.d ]; then - verbose "Install /etc/profile.d/nb-profile.sh" - echo "[ -r $NB_ROOT/etc/profile.sh ] && . $NB_ROOT/etc/profile.sh" > /tmp/nb-profile.sh \ - && $install -c /tmp/nb-profile.sh /etc/profile.d/nb-profile.sh \ - && chmod 755 /etc/profile.d/nb-profile.sh \ - ; - rm -f /tmp/nb-profile.sh + if fdiff $TMP.profile /etc/profile.d/nb-profile.sh; then + verbose "Install /etc/profile.d/nb-profile.sh" + $install -c $TMP.profile /etc/profile.d/nb-profile.sh + chmod 755 /etc/profile.d/nb-profile.sh + fi + +elif [ -w /etc/profile ]; then + + if ! grep -qm1 -F "$(cat $TMP.profile)" /etc/profile; then + verbose "Install /etc/profile" + cat $TMP.profile >> /etc/profile + fi + fi + +# +# Bye +# +rm -f $TMP* -- 2.47.3