From 1d061356cccd43eaebc4ee20e692f831d40e1898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Choch=C3=B3=C5=82?= Date: Tue, 6 Nov 2018 17:37:02 +0100 Subject: [PATCH] Finished rewriting enabling and disabling commands --- bmotd.sh | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/bmotd.sh b/bmotd.sh index 3fbaa93..929d9d7 100755 --- a/bmotd.sh +++ b/bmotd.sh @@ -6,7 +6,7 @@ # Config MOTD="/etc/motd2" INSTPATH="/etc/bmotd.sh" -CACHEPATH="/var/cache/bmotd/" +CACHEPATH="/var/cache/bmotd" # Check if root @@ -17,12 +17,11 @@ main () { fi # check if root - isRoot - if [[ $? -ne 0 ]]; then - echo 'Script needs root privileges to alter motd. Re-run with sudo.' - exit 0 + if ! isRoot; then + echo 'Script needs root privileges to alter motd. Re-run with sudo.' + exit 1 fi - + # check arguments case "$1" in enable) @@ -32,13 +31,19 @@ main () { cp "$(readlink -f $0)" "$INSTPATH" # check if crontab present - command -v crontab >/dev/null 2>&1 || ( echo "Error: couldn't find crontab. Exiting..."; exit 1) + if ! command -v crontab >/dev/null 2>&1; then + echo "Error: couldn't find crontab. Exiting..." + exit 1 + fi # Check if motd writable - test -w $MOTD || ( echo "Unable to write $MOTD . To enable update permissions. Exiting..."; exit 1 ) + if ! test -w $MOTD; then + echo "Unable to write $MOTD . To enable update permissions. Exiting..." + exit 1 + fi # Check if script is already in crontab, if not add itself - CRON_ENTRY="* * * * * $INSTPATH" + CRON_ENTRY="* * * * * $INSTPATH update" crontab -l 2>/dev/null | grep -q "$CRON_ENTRY" || ( crontab -l 2>/dev/null; echo "$CRON_ENTRY" ) | crontab - echo -e "Success! Check it yourself with crontab -l" @@ -47,12 +52,21 @@ main () { exit 0 ;; disable) - echo -e "\nDisabling PISS" - ( crontab -l 2>/dev/null | egrep -v '.*piss\.sh$'; ) | crontab - + echo -e "\nDisabling BetterMOTD" + ( crontab -l 2>/dev/null | egrep -v "$INSTPATH update$"; ) | crontab - + rm $INSTPATH + exit 0 + ;; + update) + update exit 0 ;; + *) + printHelp esac - +} + +update() { # Gather information HOSTNAME=$(hostname -s) FQDN=$(hostname -f) @@ -61,7 +75,7 @@ main () { EXTERNAL=$(dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | tr -d '"') # Get number of updates awaiting - test -f ~/.pisscache || PKG_REFRESH=1 + test -f "$CACHEPATH/pkg_count" || PKG_REFRESH=1 [[ $(date +%H%M) == "1510" ]] && PKG_REFRESH=1 if [ $PKG_REFRESH ]; then case "$OSID" in @@ -74,13 +88,13 @@ main () { *) PKG_NU="-1" esac - echo $PKG_NU > ~/.pisscache + mkdir -p "$CACHEPATH" + echo $PKG_NU > "$CACHEPATH/pkg_count" fi - PKG_NU=$(cat ~/.pisscache) + PKG_NU=$(cat $CACHEPATH/pkg_count) # Test if /etc/motd writable and run motd generator if so test -w $MOTD && motdGen - } function printHelp {