fix newlines in output
[log-quiet] / log-once
index 5980b811423413ed01a33222112313b42b73ae94..8f2fafeab79c16e5f879db96f836ca1e38d057c3 100755 (executable)
--- a/log-once
+++ b/log-once
@@ -17,7 +17,7 @@ append() {
   cat >> "$1"
 }
 log-once() {
-  local cbase c log x i out file
+  local cbase c log x i out file o
   cbase=/var/local/cron-errors
   [[ $EUID == 0 ]] || cbase=$HOME/cron-errors
   local help="Usage: log-once [OPTION]... LOG_NAME [LOG_MESSAGE]
@@ -60,7 +60,7 @@ state in the same directory.
     # read stdin for anything which is not just a newline
   elif [[ ! -t 0 ]]; then
     while read -r x; do
-      output+=( $x )
+      output+=( "$x" )
       [[ $x ]] && log=true
     done
   fi
@@ -80,12 +80,17 @@ state in the same directory.
       fi
     else
       file=${c}1
+      if (( errors == 1 )); then
+        out="tee -a"
+      fi
     fi
     $out $file <<<"log-once: $(date "+%A, %B %d, %r")"
     if [[ $2 ]]; then
       $out $file <<<"$2"
     else
-      $out $file <<<"${output[@]}"
+      for o in "${output[@]}"; do
+        $out $file <<<"$o"
+      done
       $out $file
     fi
     return 0