From 19694da937d2e8a8cf0d9eaa8cf6aa5e5abbf1f7 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Mon, 20 Jun 2016 22:42:44 -0700 Subject: [PATCH] function is useful, bring it back --- logq-function | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 logq-function diff --git a/logq-function b/logq-function new file mode 100755 index 0000000..8f73479 --- /dev/null +++ b/logq-function @@ -0,0 +1,55 @@ +#!/bin/bash +logq () { + 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." + + if [[ $1 == --help || $1 == -h ]]; then + echo "$help" + return + fi + if [[ $# == 0 ]]; then + echo "error: need 1 or more arguments +$help" + 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 + + 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" + + 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" + + if [[ $logq_ret != 0 ]]; then + x="tail -n 100 $file" + if [[ $- != *x* ]]; then + echo "logq failure. $x :" + fi + $x + fi + return $logq_ret +} -- 2.30.2