From ab0f5e6c77a5d34f9f4730f19dfe7ab0724ccae1 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Tue, 8 Oct 2024 15:34:06 +0200 Subject: [PATCH] bin/crontab_install --- bin/crontab_install | 119 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100755 bin/crontab_install diff --git a/bin/crontab_install b/bin/crontab_install new file mode 100755 index 00000000..b20198ed --- /dev/null +++ b/bin/crontab_install @@ -0,0 +1,119 @@ +#!/usr/bin/env bash +################################################################################# +# +# NB 08.10.24 - (C) 2024 Nicolas Boisselier +# +################################################################################# + +set -o errexit +declare -r NAME="$(basename "${0}")" +#declare -r DIR_NAME="$(cd "$(dirname "${0}")"; echo $(pwd))" + +################################################################################ +# +# Default value options +# +################################################################################# +VERBOSE=1 +DEBUG=0 + +################################################################################ +# +# Functions +# +################################################################################# +usage() { +echo " +=head1 NAME + +${NAME} + +=head1 SYNOPSIS + +Install ~/.crontab* files + + -v, --verbose Print verbose mode + -d, --debug Print debug messages + -h, --help Print this help (alternatives: --man, --help2man) + +=head1 DESCRIPTION + +Long description + +=head1 SEE ALSO + +http://blog.nbdom.net/ + +=head1 LICENSE + +Copyright (C) 2024 Nicolas Boisselier + +This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + +See . + +=head1 AUTHOR + +Nicolas Boisselier +" +} + +cat_crontab() { + local f + for f in .crontab $( + (hostname -f; hostname -s) \ + | awk '!x[$1]++ && "$1"!="localhost" && !/^ *$/{print ".crontab-host."$1}' \ + ); do + [ "$DEBUG" = "0" ] || echo "DEBUG: $f $([ -r "$f" ] || echo "NOT ")EXISTS" 1>&2 + [ -r "$f" ] || continue + echo "#---------------------------------------" + echo "# >>> $f" + echo "#---------------------------------------" + cat "$f" + echo "#---------------------------------------" + echo "# <<< $f" + echo "#---------------------------------------" + done + +} + +################################################################################# +# +# Args +# +################################################################################# +#shopt -s extglob +#${1?"Usage: $0 ARGUMENT"} +while [ $# -gt 0 ]; do + + case "$1" in + + -*help|-h) usage | pod2text --width 250; exit 0 ;; + --man) usage | pod2man | man -l -; exit 0 ;; + --help2man) usage | pod2man; exit 0 ;; + + --verbose|-v) VERBOSE=$(($VERBOSE+1)) ;; + + --debug|-d) DEBUG=$(($DEBUG+1)) ;; + + *) echo "Unknow option: $1 at $0!"; exit -1; ;; + + esac + shift + +done + +################################################################################# +# +# Main +# +################################################################################# +cd ~ || exit +which crontab > /dev/null || exit +CRON="$(cat_crontab)" +[ -n "$CRON" ] || exit +[ "$(crontab -l)" != "$CRON" ] || exit +[ "$VERBOSE" -gt "0" ] && echo "Update crontab" +#echo "<$CRON>" +echo "$CRON" | crontab + -- 2.47.3