From 30580daa74ba5ab7af3e02f09dfa46f1a5fe1989 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rg=20Sachse?= <joerg.sachse@slub-dresden.de>
Date: Thu, 26 Jan 2023 09:07:37 +0100
Subject: [PATCH] refactor: simpler find conditions

---
 files/usr/local/bin/move_old_logs.sh | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/files/usr/local/bin/move_old_logs.sh b/files/usr/local/bin/move_old_logs.sh
index b489aee..25377bc 100644
--- a/files/usr/local/bin/move_old_logs.sh
+++ b/files/usr/local/bin/move_old_logs.sh
@@ -8,7 +8,9 @@ START_YEAR="2015"
 CURRENT_YEAR="$( date +%Y )"
 PREVIOUS_YEAR="$(( CURRENT_YEAR - 1 ))"
 
-[[ -n ${1} ]] && APP=${1}
+[[ -n ${1} ]] && APP="${1}"
+# shellcheck disable=SC2016
+# ...because we don't want the expansion to happen in the log message
 [[ (! ${APP} =~ "disapp") && (! ${APP} =~ "subapp") && (! ${APP} =~ "subapp_webservice") ]] && \
 	echo 'ERROR: $1 needs to be one of "disapp", "subapp" or "subapp_webservice"'
 
@@ -23,19 +25,20 @@ chown -R processing.processing "/var/log/${APP}/old/"
 # move all old logfiles
 for YEAR in $( seq ${START_YEAR} ${PREVIOUS_YEAR} ); do
 	if [[ "${APP}" == "disapp" ]]; then
-		if [[ -n $( find ./ -maxdepth 1 -name "disapp.log.${YEAR}-*.lz" ) ]]; then mv "disapp.log.${YEAR}-*.lz" "old/${YEAR}/"; fi
+		if find ./ -maxdepth 1 -name "disapp.log.${YEAR}-*.lz"; then mv "disapp.log.${YEAR}-*.lz" "old/${YEAR}/"; fi
 	fi
 
 	if [[ "${APP}" == "subapp" ]]; then
-		if [[ -n $( find ./ -maxdepth 1 -name "Protokoll_SLUBArchiv_Erfolgreich-${YEAR}*.log" ) ]]; then mv "Protokoll_SLUBArchiv_Erfolgreich-${YEAR}*.log" "old/${YEAR}/"; fi
-		if [[ -n $( find ./ -maxdepth 1 -name "Protokoll_SLUBArchiv_FEHLER-${YEAR}*.log" ) ]]; then mv "Protokoll_SLUBArchiv_FEHLER-${YEAR}*.log" "old/${YEAR}/"; fi
-		if [[ -n $( find ./ -maxdepth 1 -name "sips.log.${YEAR}-*.lz" ) ]]; then mv "sips.log.${YEAR}-*.lz" "old/${YEAR}/"; fi
-		if [[ -n $( find ./ -maxdepth 1 -name "subapp.log.${YEAR}-*.lz" ) ]]; then mv "subapp.log.${YEAR}-*.lz" "old/${YEAR}/"; fi
-		if [[ -n $( find ./ -maxdepth 1 -name "producer_mails.log.${YEAR}-*.lz" ) ]]; then mv "producer_mails.log.${YEAR}-*.lz" "old/${YEAR}/"; fi
-		if [[ -n $( find ./ -maxdepth 1 -name "staff_mails.log.${YEAR}-*.lz" ) ]]; then mv "staff_mails.log.${YEAR}-*.lz" "old/${YEAR}/"; fi
+		# We run `find` before `mv` to make sure that `mv` doesn't fail if there are no files to move
+		if find ./ -maxdepth 1 -name "Protokoll_SLUBArchiv_Erfolgreich-${YEAR}*.log" > /dev/null; then mv "Protokoll_SLUBArchiv_Erfolgreich-${YEAR}*.log" "old/${YEAR}/"; fi
+		if find ./ -maxdepth 1 -name "Protokoll_SLUBArchiv_FEHLER-${YEAR}*.log" > /dev/null; then mv "Protokoll_SLUBArchiv_FEHLER-${YEAR}*.log" "old/${YEAR}/"; fi
+		if find ./ -maxdepth 1 -name "sips.log.${YEAR}-*.lz" > /dev/null; then mv "sips.log.${YEAR}-*.lz" "old/${YEAR}/"; fi
+		if find ./ -maxdepth 1 -name "subapp.log.${YEAR}-*.lz" > /dev/null; then mv "subapp.log.${YEAR}-*.lz" "old/${YEAR}/"; fi
+		if find ./ -maxdepth 1 -name "producer_mails.log.${YEAR}-*.lz" > /dev/null; then mv "producer_mails.log.${YEAR}-*.lz" "old/${YEAR}/"; fi
+		if find ./ -maxdepth 1 -name "staff_mails.log.${YEAR}-*.lz" > /dev/null; then mv "staff_mails.log.${YEAR}-*.lz" "old/${YEAR}/"; fi
 	fi
 
 	if [[ "${APP}" == "subapp_webservice" ]]; then
-		if [[ -n $( find ./ -maxdepth 1 -name "webservice.log.${YEAR}-*.lz" ) ]]; then mv "webservice.log.${YEAR}-*.lz" "old/${YEAR}/"; fi
+		if find ./ -maxdepth 1 -name "webservice.log.${YEAR}-*.lz"; then mv "webservice.log.${YEAR}-*.lz" "old/${YEAR}/"; fi
 	fi
 done
-- 
GitLab