change indent level to 2
authorIan Kelling <iank@fsf.org>
Mon, 5 Aug 2019 03:23:28 +0000 (23:23 -0400)
committerIan Kelling <iank@fsf.org>
Mon, 5 Aug 2019 03:23:28 +0000 (23:23 -0400)
failmail
log-once
logmail
logq
logq-function
setup
sysd-mail-once

index 703e13038d6c7428d32e9c4a3f21f6d84966317b..377564e294b6d5a038b1bec39352f84de32ace15 100755 (executable)
--- a/failmail
+++ b/failmail
 # limitations under the License.
 
 case $1 in
 # limitations under the License.
 
 case $1 in
-    -h|--help)
-        cat <<'EOF'
+  -h|--help)
+    cat <<'EOF'
 usage: logmail CMD [ARGS...]
 
 Send mail in case of failure of CMD
 EOF
 usage: logmail CMD [ARGS...]
 
 Send mail in case of failure of CMD
 EOF
-        exit 0
-        ;;
+    exit 0
+    ;;
 esac
 
 t=$(mktemp)
 if ! "$@" &>"$t"; then
 esac
 
 t=$(mktemp)
 if ! "$@" &>"$t"; then
-    mail -s "$HOSTNAME: $*" $USER@localhost <"$t"
-    rm "$t"
+  mail -s "$HOSTNAME: $*" $USER@localhost <"$t"
+  rm "$t"
 fi
 exit 0
 fi
 exit 0
index 179dacfd8c670eea5035a2c6255d7a9e60a727c5..5980b811423413ed01a33222112313b42b73ae94 100755 (executable)
--- a/log-once
+++ b/log-once
 # limitations under the License.
 
 append() {
 # limitations under the License.
 
 append() {
-    cat >> "$1"
+  cat >> "$1"
 }
 log-once() {
 }
 log-once() {
-    local cbase c log x i out file
-    cbase=/var/local/cron-errors
-    [[ $EUID == 0 ]] || cbase=$HOME/cron-errors
-    local help="Usage: log-once [OPTION]... LOG_NAME [LOG_MESSAGE]
+  local cbase c log x i out file
+  cbase=/var/local/cron-errors
+  [[ $EUID == 0 ]] || cbase=$HOME/cron-errors
+  local help="Usage: log-once [OPTION]... LOG_NAME [LOG_MESSAGE]
 
 For cronjobs, email log on repeated failure and success after failure.
 
 
 For cronjobs, email log on repeated failure and success after failure.
 
@@ -34,68 +34,68 @@ or $HOME/cron-errors if not root, and keeps
 state in the same directory.
 
 -ERRORS:  ERRORS is the number of errors to accumulate before outputing the error"
 state in the same directory.
 
 -ERRORS:  ERRORS is the number of errors to accumulate before outputing the error"
-    errors=3
-    while true; do
-        if [[ $1 == --help ]]; then
-            echo "$help"
-            return
-        elif [[ $1 == -[0-9]* ]]; then
-            errors=${1#-}
-            shift
-        elif [[ $1 == -- ]]; then
-            shift
-            break
-        else
-            break
-        fi
-    done
-    log_name=$1
-    # todo, make option & make them overridable based on command line or env variable
-    [[ -d $cbase ]] || mkdir -p $cbase
-    c=$cbase/$log_name
-    # http://stackoverflow.com/questions/2456750/detect-presence-of-stdin-contents-in-shell-script
-    log=false
-    if [[ $2 ]]; then
-        log=true
-        # read stdin for anything which is not just a newline
-    elif [[ ! -t 0 ]]; then
-        while read -r x; do
-            output+=( $x )
-            [[ $x ]] && log=true
-        done
-    fi
-    glob="$c[0-9]*"
-    file=($glob); [[ $file != "$glob" ]] || file=
-    if $log; then
-        out=append
-        if [[ $file ]]; then
-            i="${file#$c}"
-            if (( i < errors )); then
-                new_file=$c$((i+1))
-                mv $file $new_file
-                file=$new_file
-                if [[ $file == $c$errors ]]; then
-                    out="tee -a"
-                fi
-            fi
-        else
-            file=${c}1
-        fi
-        $out $file <<<"log-once: $(date "+%A, %B %d, %r")"
-        if [[ $2 ]]; then
-            $out $file <<<"$2"
-        else
-            $out $file <<<"${output[@]}"
-            $out $file
-        fi
-        return 0
+  errors=3
+  while true; do
+    if [[ $1 == --help ]]; then
+      echo "$help"
+      return
+    elif [[ $1 == -[0-9]* ]]; then
+      errors=${1#-}
+      shift
+    elif [[ $1 == -- ]]; then
+      shift
+      break
+    else
+      break
     fi
     fi
+  done
+  log_name=$1
+  # todo, make option & make them overridable based on command line or env variable
+  [[ -d $cbase ]] || mkdir -p $cbase
+  c=$cbase/$log_name
+  # http://stackoverflow.com/questions/2456750/detect-presence-of-stdin-contents-in-shell-script
+  log=false
+  if [[ $2 ]]; then
+    log=true
+    # read stdin for anything which is not just a newline
+  elif [[ ! -t 0 ]]; then
+    while read -r x; do
+      output+=( $x )
+      [[ $x ]] && log=true
+    done
+  fi
+  glob="$c[0-9]*"
+  file=($glob); [[ $file != "$glob" ]] || file=
+  if $log; then
+    out=append
     if [[ $file ]]; then
     if [[ $file ]]; then
-        rm -f $file
+      i="${file#$c}"
+      if (( i < errors )); then
+        new_file=$c$((i+1))
+        mv $file $new_file
+        file=$new_file
         if [[ $file == $c$errors ]]; then
         if [[ $file == $c$errors ]]; then
-            echo "log-once success after failure for $c"
+          out="tee -a"
         fi
         fi
+      fi
+    else
+      file=${c}1
+    fi
+    $out $file <<<"log-once: $(date "+%A, %B %d, %r")"
+    if [[ $2 ]]; then
+      $out $file <<<"$2"
+    else
+      $out $file <<<"${output[@]}"
+      $out $file
     fi
     return 0
     fi
     return 0
+  fi
+  if [[ $file ]]; then
+    rm -f $file
+    if [[ $file == $c$errors ]]; then
+      echo "log-once success after failure for $c"
+    fi
+  fi
+  return 0
 }
 log-once "$@"
 }
 log-once "$@"
diff --git a/logmail b/logmail
index cac1e21372cb80389189987d2910279e125b235e..4aad395591d9f123072ef545e7d25f5cce937d53 100755 (executable)
--- a/logmail
+++ b/logmail
 # limitations under the License.
 
 case $1 in
 # limitations under the License.
 
 case $1 in
-    -h|--help)
-        cat <<'EOF'
+  -h|--help)
+    cat <<'EOF'
 usage: logmail CMD [ARGS...]
 
 Send mail in case of output or failure of CMD
 EOF
 usage: logmail CMD [ARGS...]
 
 Send mail in case of output or failure of CMD
 EOF
-        exit 0
-        ;;
+    exit 0
+    ;;
 esac
 t=$(mktemp)
 if ! "$@" &>"$t" || [[ ! -s $t ]]; then
 esac
 t=$(mktemp)
 if ! "$@" &>"$t" || [[ ! -s $t ]]; then
-    mail -s "$HOSTNAME: $*" $USER@localhost <"$t"
-    rm "$t"
+  mail -s "$HOSTNAME: $*" $USER@localhost <"$t"
+  rm "$t"
 fi
 exit 0
 fi
 exit 0
diff --git a/logq b/logq
index b253bef510416c8f9a256b2d2599d090ae81b27c..08b059d70048b0f834cdaa6c98350cef717613d0 100755 (executable)
--- a/logq
+++ b/logq
 # This file is exactly the same as logq-function except the last line
 # to make it into a script.
 logq () {
 # This file is exactly the same as logq-function except the last line
 # to make it into a script.
 logq () {
-    local help="Usage: logq [-h|--help] COMMAND [ARG...]
+  local help="Usage: logq [-h|--help] COMMAND [ARG...]
 
 Log to temp file, output tail of log in case of error.
 
 Run COMMAND with ARGs. Output return code, command, and log path. Tail
 log if there is a failure.  Follows output format of logsave."
 
 
 Log to temp file, output tail of log in case of error.
 
 Run COMMAND with ARGs. Output return code, command, and log path. Tail
 log if there is a failure.  Follows output format of logsave."
 
-    if [[ $1 == --help || $1 == -h ]]; then
-        echo "$help"
-        return
-    fi
-    if [[ $# == 0 ]]; then
-       echo "error: need 1 or more arguments
+  if [[ $1 == --help || $1 == -h ]]; then
+    echo "$help"
+    return
+  fi
+  if [[ $# == 0 ]]; then
+    echo "error: need 1 or more arguments
 $help"
 $help"
-       return 1
-    fi
+    return 1
+  fi
 
 
-    # deliniate arguments, so spaces aren't ambiguous
-    local index=0
-    local x prettycommand
-    for x in "$@"; do
-        prettycommand+="[$index]$x "
-        index=$(( index+1 ))
-    done
+  # deliniate arguments, so spaces aren't ambiguous
+  local index=0
+  local x prettycommand
+  for x in "$@"; do
+    prettycommand+="[$index]$x "
+    index=$(( index+1 ))
+  done
 
 
-    local file="$*"
-    file="$(mktemp -d)/${file//[[:space:]\/]/_}"
-    # give us ~20 char filename max
-    file="${file:0:40}"
+  local file="$*"
+  file="$(mktemp -d)/${file//[[:space:]\/]/_}"
+  # give us ~20 char filename max
+  file="${file:0:40}"
 
 
-    printf "%s\n%s\n\n" "Log of $prettycommand" "$(date)" >"$file"
+  printf "%s\n%s\n\n" "Log of $prettycommand" "$(date)" >"$file"
 
 
-    if [[ $- != *x* ]]; then
-        echo "log $file = $@"
-    fi
+  if [[ $- != *x* ]]; then
+    echo "log $file = $@"
+  fi
 
 
-    # we will propagate any errors
-    local logq_ret=$(
-        set +e
-        trap ERR
-        "$@" &>> "$file"
-        echo $?
-          )
-    printf "\n%s\n%s\n" "$(date)" "----------------" >> "$file"
+  # we will propagate any errors
+  local logq_ret=$(
+    set +e
+    trap ERR
+    "$@" &>> "$file"
+    echo $?
+        )
+  printf "\n%s\n%s\n" "$(date)" "----------------" >> "$file"
 
 
-    if [[ $logq_ret != 0 ]]; then
-        x="tail -n 100 $file"
-        if [[ $- != *x* ]]; then
-            echo "logq failure. $x :"
-        fi
-        $x
+  if [[ $logq_ret != 0 ]]; then
+    x="tail -n 100 $file"
+    if [[ $- != *x* ]]; then
+      echo "logq failure. $x :"
     fi
     fi
-    return $logq_ret
+    $x
+  fi
+  return $logq_ret
 }
 logq "$@"
 }
 logq "$@"
index d07cba078045114da6555a5d7c7e96146d00e9e5..2ae5dae47fb8ff3e14890e34760473b220837b47 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 logq () {
 # See the License for the specific language governing permissions and
 # limitations under the License.
 logq () {
-    local help="Usage: logq [-h|--help] COMMAND [ARG...]
+  local help="Usage: logq [-h|--help] COMMAND [ARG...]
 
 Log Quietly. Run COMMAND with ARGs, log to temp file. Output return code,
 command, and log path. Tail log if there is a failure.
 Follows output format of logsave."
 
 
 Log Quietly. Run COMMAND with ARGs, log to temp file. Output return code,
 command, and log path. Tail log if there is a failure.
 Follows output format of logsave."
 
-    if [[ $1 == --help || $1 == -h ]]; then
-        echo "$help"
-        return
-    fi
-    if [[ $# == 0 ]]; then
-       echo "error: need 1 or more arguments
+  if [[ $1 == --help || $1 == -h ]]; then
+    echo "$help"
+    return
+  fi
+  if [[ $# == 0 ]]; then
+    echo "error: need 1 or more arguments
 $help"
 $help"
-       return 1
-    fi
+    return 1
+  fi
 
 
-    # deliniate arguments, so spaces aren't ambiguous
-    local index=0
-    local x prettycommand
-    for x in "$@"; do
-        prettycommand+="[$index]$x "
-        index=$(( index+1 ))
-    done
+  # deliniate arguments, so spaces aren't ambiguous
+  local index=0
+  local x prettycommand
+  for x in "$@"; do
+    prettycommand+="[$index]$x "
+    index=$(( index+1 ))
+  done
 
 
-    local file="$*"
-    file="$(mktemp -d)/${file//[[:space:]\/]/_}"
-    # give us ~20 char filename max
-    file="${file:0:40}"
+  local file="$*"
+  file="$(mktemp -d)/${file//[[:space:]\/]/_}"
+  # give us ~20 char filename max
+  file="${file:0:40}"
 
 
-    printf "%s\n%s\n\n" "Log of $prettycommand" "$(date)" >"$file"
+  printf "%s\n%s\n\n" "Log of $prettycommand" "$(date)" >"$file"
 
 
-    if [[ $- != *x* ]]; then
-        echo "log $file = $@"
-    fi
+  if [[ $- != *x* ]]; then
+    echo "log $file = $@"
+  fi
 
 
-    # we will propagate any errors
-    local logq_ret=$(
-        set +e
-        trap ERR
-        "$@" &>> "$file"
-        echo $?
-          )
-    printf "\n%s\n%s\n" "$(date)" "----------------" >> "$file"
+  # we will propagate any errors
+  local logq_ret=$(
+    set +e
+    trap ERR
+    "$@" &>> "$file"
+    echo $?
+        )
+  printf "\n%s\n%s\n" "$(date)" "----------------" >> "$file"
 
 
-    if [[ $logq_ret != 0 ]]; then
-        x="tail -n 100 $file"
-        if [[ $- != *x* ]]; then
-            echo "logq failure. $x :"
-        fi
-        $x
+  if [[ $logq_ret != 0 ]]; then
+    x="tail -n 100 $file"
+    if [[ $- != *x* ]]; then
+      echo "logq failure. $x :"
     fi
     fi
-    return $logq_ret
+    $x
+  fi
+  return $logq_ret
 }
 }
diff --git a/setup b/setup
index 44ef3f548c02dcc5e3dacb9bc2135036daf9de99..7ce3624fb7aee0f9be68f6c146897c682f002868 100755 (executable)
--- a/setup
+++ b/setup
@@ -19,7 +19,7 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 [[ $EUID == 0 ]] || exec sudo "$BASH_SOURCE" "$@"
 
 usage() {
 [[ $EUID == 0 ]] || exec sudo "$BASH_SOURCE" "$@"
 
 usage() {
-    cat <<EOF
+  cat <<EOF
 Usage: ${0##*/} []
 Install or uninstall files to /usr/local/bin
 
 Usage: ${0##*/} []
 Install or uninstall files to /usr/local/bin
 
@@ -29,20 +29,20 @@ Install or uninstall files to /usr/local/bin
 
 Note: Uses GNU getopt options parsing style
 EOF
 
 Note: Uses GNU getopt options parsing style
 EOF
-    exit $1
+  exit $1
 }
 dry=false
 uninstall=false # default
 temp=$(getopt -l help,uninstall,dry-run hun "$@") || usage 1
 eval set -- "$temp"
 while true; do
 }
 dry=false
 uninstall=false # default
 temp=$(getopt -l help,uninstall,dry-run hun "$@") || usage 1
 eval set -- "$temp"
 while true; do
-    case $1 in
-        -n|--dry-run) dry=true; shift ;;
-        -u|--uninstall) uninstall=true; shift ;;
-        -h|--help) usage ;;
-        --) shift; break ;;
-        *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
-    esac
+  case $1 in
+    -n|--dry-run) dry=true; shift ;;
+    -u|--uninstall) uninstall=true; shift ;;
+    -h|--help) usage ;;
+    --) shift; break ;;
+    *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
+  esac
 done
 
 
 done
 
 
@@ -50,23 +50,23 @@ x="$(readlink -f "$BASH_SOURCE")"; cd ${x%/*} # directory of this file
 
 files=()
 for f in *; do
 
 files=()
 for f in *; do
-    if [[ -x $f && ! -d $f && ! -L $f && $f != setup ]]; then
-        files+=($f)
-    fi
+  if [[ -x $f && ! -d $f && ! -L $f && $f != setup ]]; then
+    files+=($f)
+  fi
 done
 
 if $uninstall; then
 done
 
 if $uninstall; then
-    if $dry; then
-        echo "setup dry run: cd /usr/local/bin"
-        echo "setup dry run: rm -fv ${files[*]}"
-    else
-        cd /usr/local/bin
-        rm -fv ${files[@]}
-    fi
+  if $dry; then
+    echo "setup dry run: cd /usr/local/bin"
+    echo "setup dry run: rm -fv ${files[*]}"
+  else
+    cd /usr/local/bin
+    rm -fv ${files[@]}
+  fi
 else
 else
-    if $dry; then
-        echo "setup dry run: install -v ${files[*]} /usr/local/bin"
-    else
-        install -v ${files[@]} /usr/local/bin
-    fi
+  if $dry; then
+    echo "setup dry run: install -v ${files[*]} /usr/local/bin"
+  else
+    install -v ${files[@]} /usr/local/bin
+  fi
 fi
 fi
index 6e6807686a43c3e7f78a6c93b556533da9e8c02a..8feedd377e6324d4d70b5e87f3566495c80fcec9 100755 (executable)
@@ -20,8 +20,8 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 errors=3
 cbase=$HOME/sysd-mail-once-state
 case "$1" in
 errors=3
 cbase=$HOME/sysd-mail-once-state
 case "$1" in
-    -h|--help)
-        cat <<EOF
+  -h|--help)
+    cat <<EOF
 Usage: sysd-log-once [-ERRORS] SERVICE COMMAND [ARGS...]
 For systemd timers, email on repeated failure & success after failure.
 
 Usage: sysd-log-once [-ERRORS] SERVICE COMMAND [ARGS...]
 For systemd timers, email on repeated failure & success after failure.
 
@@ -34,12 +34,12 @@ Stores error counts in $cbase
 -ERRORS:  ERRORS is the number of failurs to accumulate before mailing the error.
           Default is 3.
 EOF
 -ERRORS:  ERRORS is the number of failurs to accumulate before mailing the error.
           Default is 3.
 EOF
-        exit 0
-        ;;
-    -[0-9]*)
-        errors=${1#-}
-        shift
-        ;;
+    exit 0
+    ;;
+  -[0-9]*)
+    errors=${1#-}
+    shift
+    ;;
 esac
 service=$1
 shift
 esac
 service=$1
 shift
@@ -52,29 +52,29 @@ u=${USER:-root}
 [[ -d $cbase ]] || mkdir -p $cbase
 
 if [[ ! $file ]]; then
 [[ -d $cbase ]] || mkdir -p $cbase
 
 if [[ ! $file ]]; then
-    cursor=$(journalctl --show-cursor -qn0|sed 's/^\s*--\scursor:\s*//')
+  cursor=$(journalctl --show-cursor -qn0|sed 's/^\s*--\scursor:\s*//')
 fi
 if "$@"; then
 fi
 if "$@"; then
-    if [[ $file ]]; then
-        rm -f $file
-        if [[ $file == $c$errors ]]; then
-            echo | mail -s "$HOSTNAME: $service success" $u@localhost
-        fi
+  if [[ $file ]]; then
+    rm -f $file
+    if [[ $file == $c$errors ]]; then
+      echo | mail -s "$HOSTNAME: $service success" $u@localhost
     fi
     fi
+  fi
 else # $@ failed
 else # $@ failed
-    if [[ $file ]]; then
-        i=${file#$c}
-        if (( i < errors )); then
-            new_file=$c$((i+1))
-            mv $file $new_file
-            file=$new_file
-            if [[ $file == $c$errors ]]; then
-                journalctl -u $service.service --after-cursor=$(<$file) | \
-                    mail -s "$HOSTNAME: $service failure" $u@localhost
-            fi
-        fi
-    else
-        file=${c}1
-        printf "%s\n" "$cursor" >$file
+  if [[ $file ]]; then
+    i=${file#$c}
+    if (( i < errors )); then
+      new_file=$c$((i+1))
+      mv $file $new_file
+      file=$new_file
+      if [[ $file == $c$errors ]]; then
+        journalctl -u $service.service --after-cursor=$(<$file) | \
+          mail -s "$HOSTNAME: $service failure" $u@localhost
+      fi
     fi
     fi
+  else
+    file=${c}1
+    printf "%s\n" "$cursor" >$file
+  fi
 fi
 fi