function is useful, bring it back
[log-quiet] / logq-function
1 #!/bin/bash
2 logq () {
3 local help="Usage: logq [-h|--help] COMMAND [ARG...]
4
5 Log Quietly. Run COMMAND with ARGs, log to temp file. Output return code,
6 command, and log path. Tail log if there is a failure.
7 Follows output format of logsave."
8
9 if [[ $1 == --help || $1 == -h ]]; then
10 echo "$help"
11 return
12 fi
13 if [[ $# == 0 ]]; then
14 echo "error: need 1 or more arguments
15 $help"
16 return 1
17 fi
18
19 # deliniate arguments, so spaces aren't ambiguous
20 local index=0
21 local x prettycommand
22 for x in "$@"; do
23 prettycommand+="[$index]$x "
24 index=$(( index+1 ))
25 done
26
27 local file="$*"
28 file="$(mktemp -d)/${file//[[:space:]\/]/_}"
29 # give us ~20 char filename max
30 file="${file:0:40}"
31
32 printf "%s\n%s\n\n" "Log of $prettycommand" "$(date)" >"$file"
33
34 if [[ $- != *x* ]]; then
35 echo "log $file = $@"
36 fi
37
38 # we will propagate any errors
39 local logq_ret=$(
40 set +e
41 trap ERR
42 "$@" &>> "$file"
43 echo $?
44 )
45 printf "\n%s\n%s\n" "$(date)" "----------------" >> "$file"
46
47 if [[ $logq_ret != 0 ]]; then
48 x="tail -n 100 $file"
49 if [[ $- != *x* ]]; then
50 echo "logq failure. $x :"
51 fi
52 $x
53 fi
54 return $logq_ret
55 }