-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjtool-ps
executable file
·89 lines (73 loc) · 2.04 KB
/
jtool-ps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
# Usage: jtool-ps [OPTIONS]
########################################################################
# Bash environment
########################################################################
# Safety
set -o errexit \
-o pipefail \
-o nounset \
-o noclobber
# Extensions
set +o posix
shopt -s lastpipe
shopt -s expand_aliases
shopt -s extglob
shopt -s globstar
########################################################################
# Handle private protocol
########################################################################
if (( $# > 0 )); then
case $1 in
-\?) exec ps --help all ;;
-\:) exec cat <<SYNOPSYS
jtool CMD
CMD sinopsys...
SYNOPSYS
exit 0
;;
esac
fi
########################################################################
# JQ script
########################################################################
JQ_SCRIPT='/dev/fd/7' # to close: exec 7<&-
exec 7<<\JQEOF
# input is `ps` output as a big string with spaces trimmed
(./"\n") as $lines
| ($lines[0]/" ") as $names
| ($names|length) as $nfields
| $lines[1:-1][]
| (./" ") as $values
| reduce range($nfields) as $i
({};if $i+1 == $nfields
then .[$names[$i]] = ($values[$i:] | join(" "))
else .[$names[$i]] = $values[$i] end)
JQEOF
########################################################################
# Command
########################################################################
# `jq` options
declare -a JQ_OPT=(
--slurp
--raw-input
--indent 4
--from-file ${JQ_SCRIPT}
)
# Filter to trim spaces
declare -a SED_FILTER=(
-e 's/^[ \t]\+//'
-e 's/[ \t]\+$//'
-e '/^$/d'
-e 's/[ \t]\+/ /g'
)
# `ps` with JSON output!
ps "$@" \
| sed "${SED_FILTER[@]}" \
| jq "${JQ_OPT[@]}"
########################################################################
# Bugs:
# Do not manage spaces inside column values except in the last column.
########################################################################
exit
# vim:syntax=sh:et:sw=4:ts=4:ai