From bb029b824ae53507a91646a7425516b7f8c10503 Mon Sep 17 00:00:00 2001 From: Greg Anders Date: Wed, 9 Dec 2015 15:22:04 -0600 Subject: [PATCH] Optimize install script --- bin/janitor | 2 +- install.sh | 24 ++++++++---------------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/bin/janitor b/bin/janitor index 946651f..22920f3 100755 --- a/bin/janitor +++ b/bin/janitor @@ -26,7 +26,7 @@ function clean() { LOG="logs/janitor_$(date +"%Y%m%d").log" - if [ ! -e $LOG ]; then + if [ ! -e "$LOG" ]; then echo "Files removed from $TARGET_DIR on $(date +%m-%d-%Y):" > "$LOG" fi echo "${1##*/}" >> "$LOG" diff --git a/install.sh b/install.sh index d0c0480..65d8409 100755 --- a/install.sh +++ b/install.sh @@ -45,31 +45,23 @@ function create_crontab() { } function check_crontab() { - if [[ $CURRENT_CRONTAB == *"# Begin Janitor job"* && \ - $CURRENT_CRONTAB == *"# End Janitor job"* ]]; then + if [[ $1 == *"# Begin Janitor job"* && \ + $1 == *"# End Janitor job"* ]]; then return 1 else return 0 fi } - # Create crontab -CRONTAB=$(create_crontab "$TARGET_DIR" "$NUM_DAYS" "$TRASH_DIR") -CURRENT_CRONTAB="$(crontab -l 2>/dev/null)" -if [ $? -ne 0 ]; then - # No existing crontab +if check_crontab "$(crontab -l 2>/dev/null)"; then + # Crontab exists but does not already contain ours echo "Creating crontab entry." - echo "$CRONTAB"| crontab - + entry=$(create_crontab "$TARGET_DIR" "$NUM_DAYS" "$TRASH_DIR") + (crontab -l 2>/dev/null; [[ $? -eq 0 ]] && echo " " ; echo "$entry") | crontab - else - if check_crontab; then - # Crontab exists but does not already contain ours - echo "Creating crontab entry." - (crontab -l ; echo " " ; echo "$CRONTAB") | crontab - - else - echo "Janitor cronjob already exists. Remove the current job (using crontab -e) and re-run this installation script." - exit 1 - fi + echo "Janitor cronjob already exists. Remove the current job (using crontab -e) and re-run this installation script." + exit 1 fi mkdir -p logs -- 2.26.2