initial commit. working as expected
[log-quiet] / logq
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.
7
8 Minor details: Use logsave if available, which adds informational header/footer
9 to log file. Logfile is put in random temp dir, with filename made from the
10 alphanumeric characters of COMMAND + ARGs."
11
12 if [[ $1 == --help || $1 == -h ]]; then
13 echo "$help"
14 return
15 fi
16 if [[ $# == 0 ]]; then
17 echo "error: need 1 or more arguments
18 $help"
19 return 1
20 fi
21
22 local index=0
23 local x prettycommand
24 for x in "$@"; do
25 prettycommand+="[$index]$x "
26 index=$(( index+1 ))
27 done
28
29
30 local file="$*"
31 file="$(mktemp -d)/${file//[^[:alnum:]]/}"
32
33 echo "log of $prettycommand" >"$file"
34 date >>"$file"
35 echo >>"$file"
36
37 # we will propagate any errors
38 local e=$-
39 [[ $e == *e* ]] && set +e
40 "$@" &>> "$file"
41 local ret=$?
42 [[ $e == *e* ]] && set -e
43
44 echo >>"$file"
45 echo "----------------" >>"$file"
46 date >>"$file"
47 echo "end of log" >>"$file"
48
49 echo -n "\$?=$ret "
50 echo -n "$prettycommand"
51 echo "[log] $file"
52 return $ret
53 }
54 logq "$@"