From d26a7fc2ccf01a9a39e3a06b7a09fcbf31756c1d Mon Sep 17 00:00:00 2001 From: Christoph Date: Sat, 23 Dec 2017 18:45:43 +0100 Subject: [PATCH] Adjust script output in case of running as a cronjob. --- mail_to_listadmins.sh | 295 +++++++++++++++++++++++++++++------------- 1 file changed, 205 insertions(+), 90 deletions(-) diff --git a/mail_to_listadmins.sh b/mail_to_listadmins.sh index 703ee44..33ae312 100755 --- a/mail_to_listadmins.sh +++ b/mail_to_listadmins.sh @@ -50,7 +50,7 @@ echo_log_skipped() { } echononl(){ - if $terminal && $LOGGING ; then + if $terminal ; then echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo -e -n "$*\\c" 1>&2 @@ -58,6 +58,8 @@ echononl(){ echo -e -n "$*" 1>&2 fi rm /tmp/shprompt$$ + else + echo -n "$*" fi } @@ -115,7 +117,7 @@ echo_done() { } echo_failed(){ - if $terminal && $LOGGING ; then + if $terminal ; then echo -e "\033[75G[ \033[1;31mfailed\033[m ]" else echo " [ failed ]" @@ -123,7 +125,7 @@ echo_failed(){ } echo_skipped() { - if $terminal && $LOGGING ; then + if $terminal ; then echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" else echo " [ skipped ]" @@ -184,7 +186,17 @@ Optinal Control Options: Example: - $(basename $0) -b "Listsoftware has changed" -f /root/msg_listadmin.txt -s support@so36.net + - Normal use at the command line: + + ./$(basename $0) -b \"Listsoftware has changed\" -f /root/msg_listadmin.txt -s support@so36.net + + + - Use as cronjob: + + echo 'YES' | $(realpath "$0") -b \"Info E-Mail\" -p -s \"support@so36.net\" -f \"/root/bin/schleuder3/messages/msg_listadmins.txt\" + + \033[1mNote\033[m: + All options must be present, at the configuration file or at the command line. " clean_up 2 @@ -252,7 +264,7 @@ MESSAGE_FILE="" SUBJECT="" echo "" -echononl " Loading default Configuration values from $(basename ${conf_file}).." +echononl " Loading default Configuration values from $(basename ${conf_file}).." if [[ ! -f "$conf_file" ]]; then echo_skipped else @@ -265,19 +277,25 @@ else fi fi -[[ -n "$SENDER_ADDRESS" ]] && default_sender_address="$SENDER_ADDRESS" -[[ -n "$MESSAGE_FILE" ]] && default_orig_msg_file="$MESSAGE_FILE" -[[ -n "$SUBJECT" ]] && default_subject="$SUBJECT" +if $terminal ; then + [[ -n "$SENDER_ADDRESS" ]] && default_sender_address="$SENDER_ADDRESS" + [[ -n "$MESSAGE_FILE" ]] && default_orig_msg_file="$MESSAGE_FILE" + [[ -n "$SUBJECT" ]] && default_subject="$SUBJECT" +else + [[ -n "$SENDER_ADDRESS" ]] && sender_address="$SENDER_ADDRESS" + [[ -n "$MESSAGE_FILE" ]] && orig_msg_file="$MESSAGE_FILE" + [[ -n "$SUBJECT" ]] && subject="$SUBJECT" +fi #----------------------------- # Read Commandline Parameters #----------------------------- -subject="" -sender_address="" -gpg_pw="" -orig_msg_file="" +#subject="" +#sender_address="" +#gpg_pw="" +#orig_msg_file="" while getopts b:hf:p:s: opt ; do case $opt in b) subject="$OPTARG" @@ -295,7 +313,7 @@ while getopts b:hf:p:s: opt ; do done -if [[ -z "$orig_msg_file" ]] ; then +if $terminal && [[ -z "$orig_msg_file" ]] ; then echo "" echo -e "\033[32m--\033[m" echo "" @@ -321,10 +339,14 @@ if [[ -z "$orig_msg_file" ]] ; then fi if [[ ! -f "$orig_msg_file" ]]; then - fatal "Message file \033[1m$orig_msg_file\033[m not found!" + if $terminal ; then + fatal "Message file \033[1m$orig_msg_file\033[m not found!" + else + fatal "Message file $orig_msg_file not found!" + fi fi -if [[ -z "$subject" ]]; then +if $terminal && [[ -z "$subject" ]]; then echo "" echo -e "\033[32m--\033[m" echo "" @@ -361,7 +383,7 @@ if echo "$subject" 2> /dev/null | file -i - | grep -i -q utf-8 ; then fi -if [[ -z "$sender_address" ]] ; then +if $terminal && [[ -z "$sender_address" ]] ; then echo "" echo -e "\033[32m--\033[m" echo "" @@ -392,7 +414,7 @@ if ! gpg --list-secret-keys "${sender_address}" > /dev/null 2>&1 ; then fi -if [[ -z "$gpg_pw" ]]; then +if $terminal && [[ -z "$gpg_pw" ]]; then _gpg_pw_1="X" _gpg_pw_2="Y" echo "" @@ -430,61 +452,87 @@ if ! $(echo "$gpg_pw" | gpg --trust-model=always --pinentry-mode loopback -o /de fi -clear -echo "" -echo -e "\033[32m--\033[m" -echo "" -echo "" -echo -e " \033[1;32mSettings for script \033[1m$(basename "$0")\033[m" -echo "" -echo -e " Sender Address...................: $sender_address" -echo -e " E-Mail Subject...................: $subject" -if [[ -n "$subject_encoded" ]] ; then - echo -e " E-Mail Subject (utf-8 encoded)...: $subject_encoded" -fi -echo -e " File containing the message......: $orig_msg_file" -echo "" -echo -e " Message: \n\n\033[1m$(cat "$orig_msg_file")\033[m\n" -echo "" - - -echo "" -echononl "Type upper case \033[1;37mYES\033[m to continue executing this script: " -read OK -if [[ "$OK" = "YES" ]] ; then +if $terminal ; then + clear echo "" echo -e "\033[32m--\033[m" echo "" - echo -e " \033[32m----------\033[m" - echo -e " \033[1mStarting Script to inform all Listadmins\033[m" - echo -e " \033[32m----------\033[m" - echo_log "# ----------------------------------------------------------------" - echo_log "# - Log output of script '$(basename $0)' at $_date" - echo_log "# ----------------------------------------------------------------" - echo_log "" - echo_log "----------" - echo_log "Settings for script $(basename "$0"):" - echo_log "----------" - echo_log "" - echo_log "Sender Address...................: $sender_address" - echo_log "E-Mail Subject...................: $subject" - if [[ -n "$subject_encoded" ]] ; then - echo_log "E-Mail Subject (utf-8 encoded)...: $subject_encoded" - fi - echo_log "File containing the message......: $orig_msg_file" - echo_log "" - echo_log "Message: " - echo_log "--" - echo_log "$(cat "$orig_msg_file")" - echo_log "--" - + echo "" + echo -e " \033[1;32mSettings for script \033[1m$(basename "$0")\033[m" + echo "" + echo -e " Sender Address...................: $sender_address" + echo -e " E-Mail Subject...................: $subject" + if [[ -n "$subject_encoded" ]] ; then + echo -e " E-Mail Subject (utf-8 encoded)...: $subject_encoded" + fi + echo -e " File containing the message......: $orig_msg_file" + echo "" + echo -e " Message: \n\n\033[1m$(cat "$orig_msg_file")\033[m\n" else - fatal "Abort by user request - Answer as not 'YES'" -fio "" + echo "" + echo "" + echo " Settings for script $(basename "$0"):" + echo "" + echo " Sender Address...................: $sender_address" + echo " E-Mail Subject...................: $subject" + if [[ -n "$subject_encoded" ]] ; then + echo " E-Mail Subject (utf-8 encoded)...: $subject_encoded" + fi + echo " File containing the message......: $orig_msg_file" + echo "" + echo " Message: " + echo "--" + echo "$(cat "$orig_msg_file")" + echo "--" fi echo "" +if $terminal ; then + echo "" + echononl "Type upper case \033[1;37mYES\033[m to continue executing this script: " + read OK + if [[ "$OK" != "YES" ]] ; then + fatal "Abort by user request - Answer as not 'YES'" + else + echo "" + echo -e "\033[32m--\033[m" + echo "" + echo -e " \033[32m----------\033[m" + echo -e " \033[1mStarting Script to inform all Listadmins\033[m" + echo -e " \033[32m----------\033[m" + fi +else + echo "" + echo " ----------" + echo " Starting Script to inform all Listadmins" + echo " ----------" +fi + + +echo_log "# ----------------------------------------------------------------" +echo_log "# - Log output of script '$(basename $0)' at $_date" +echo_log "# ----------------------------------------------------------------" +echo_log "" +echo_log "----------" +echo_log "Settings for script $(basename "$0"):" +echo_log "----------" +echo_log "" +echo_log "Sender Address...................: $sender_address" +echo_log "E-Mail Subject...................: $subject" +if [[ -n "$subject_encoded" ]] ; then + echo_log "E-Mail Subject (utf-8 encoded)...: $subject_encoded" +fi +echo_log "File containing the message......: $orig_msg_file" +echo_log "" +echo_log "Message: " +echo_log "--" +echo_log "$(cat "$orig_msg_file")" +echo_log "--" + +echo "" + + echononl " Create log directory" if [[ ! -d "$log_dir" ]]; then mkdir "$log_dir" > /dev/null 2>&1 @@ -497,12 +545,21 @@ else echo_skipped fi -echo "" -echo "" -echo -e " \033[32m----------\033[m" -echo -e " \033[1mSummary Lists at $(hostname -f)\033[m" -echo -e " \033[32m----------\033[m" -echo "" +if $terminal ; then + echo "" + echo "" + echo -e " \033[32m----------\033[m" + echo -e " \033[1mSummary Lists at $(hostname -f)\033[m" + echo -e " \033[32m----------\033[m" + echo "" +else + echo "" + echo "" + echo " ----------" + echo " Summary Lists at $(hostname -f)" + echo " ----------" + echo "" +fi echo_log "" echo_log "" echo_log "----------" @@ -517,7 +574,11 @@ while IFS= read -r -d '' _dir ; do done < <(find "$schleuder_lists_dir" -mindepth 1 -maxdepth 1 -type d -print0) echo_log "Schleuder Domains:" -echo -e " \033[32m\033[1mSchleuder Domains\033[m:" +if $terminal ; then + echo -e " \033[32m\033[1mSchleuder Domains\033[m:" +else + echo " Schleuder Domains:" +fi for _domain in ${schleuder_domains_arr[@]} ; do echo_log " $_domain" echo " $_domain" @@ -533,7 +594,12 @@ done echo_log "" echo_log "Schleuder Lists" -echo -e "\n \033[32m\033[1mSchleuder Lists\033[m:" +if $terminal ; then + echo -e "\n \033[32m\033[1mSchleuder Lists\033[m:" +else + echo "" + echo " Schleuder Lists" +fi for _val in "${schleuder_lists_arr[@]}" ; do IFS=':' read -a _val_arr <<< "${_val}" _domain="${_val_arr[0]}" @@ -547,11 +613,29 @@ for _val in "${schleuder_lists_arr[@]}" ; do _last_domain="$_domain" done -echo "" -echo "" -echo -e " \033[32m----------\033[m" -echo -e " \033[1mMain part of script: inform list admins\033[m" -echo -e " \033[32m----------\033[m" +echo_log +echo_log "Total number of lists: ${#schleuder_lists_arr[@]}" +if $terminal ; then + echo -e "\n \033[32m\033[1mTotal number of lists:\033[m: ${#schleuder_lists_arr[@]}" +else + echo "" + echo " Total number of lists: ${#schleuder_lists_arr[@]}" +fi + +if $terminal ; then + echo "" + echo "" + echo -e " \033[32m----------\033[m" + echo -e " \033[1mMain part of script: inform list admins\033[m" + echo -e " \033[32m----------\033[m" +else + echo "" + echo "" + echo " ----------" + echo " Main part of script: inform list admins" + echo " ----------" +fi + echo_log "" echo_log "" echo_log "----------" @@ -587,7 +671,13 @@ for _val in "${schleuder_lists_arr[@]}" ; do echo_log "" echo_log "${_list}@${_domain}:" - echo -e "\n \033[32m\033[1m${_domain} - ${_list}\033[m:" + + if $terminal ; then + echo -e "\n \033[32m\033[1m${_domain} - ${_list}\033[m:" + else + echo "" + echo " ${_list}@${_domain}:" + fi # - Copy original message in tmp folder ($LOCK_DIR) # - @@ -782,31 +872,56 @@ else echo_done fi -echo "" -echo "" -echo -e " \033[32m----------\033[m" -echo -e " \033[1mStatistics \033[m" -echo -e " \033[32m----------\033[m" -echo "" -echo -e " ${#lists_done[@]} lists successfully informed" -echo -e " ${#lists_warn[@]} lists with warnings" -echo -e " ${#lists_failed[@]} lists failed" +if $terminal ; then + echo "" + echo "" + echo -e " \033[32m----------\033[m" + echo -e " \033[1mStatistics \033[m" + echo -e " \033[32m----------\033[m" + echo "" + echo -e " Total number of lists: ${#schleuder_lists_arr[@]}" + echo "" + echo -e " ${#lists_done[@]} lists successfully informed" + echo -e " ${#lists_warn[@]} lists with warnings" + echo -e " ${#lists_failed[@]} lists failed" +else + echo "" + echo "" + echo " ----------" + echo " Statistics" + echo " ----------" + echo "" + echo " Total number of lists: ${#schleuder_lists_arr[@]}" + echo "" + echo " ${#lists_done[@]} lists successfully informed" + echo " ${#lists_warn[@]} lists with warnings" + echo " ${#lists_failed[@]} lists failed" +fi + echo_log "" echo_log "" echo_log "----------" echo_log "Statistics" echo_log "----------" echo_log +echo_log "Total number of lists: ${#schleuder_lists_arr[@]}" +echo_log echo_log "${#lists_done[@]} lists successfully informed" echo_log "${#lists_warn[@]} lists with warnings" echo_log "${#lists_failed[@]} lists failed" echo_log if [[ ${#lists_failed[@]} -gt 0 ]]; then echo_log "Listadmin(s) not informed:" - echo -e "\n \033[1mListadmin(s) not informed:\033[m" + if $terminal ; then + echo -e "\n \033[1mListadmin(s) not informed:\033[m" + else + echo "" + echo " Listadmin(s) not informed:" + fi + for _list in "${lists_failed[@]}" ; do echo -e " $_list" - echo_log " $_list" + echo_log " $_list" done echo_log info "See also log file: $log_file" @@ -817,4 +932,4 @@ fi echo "" clean_up 0 - +