-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjtool-ls
executable file
·98 lines (81 loc) · 2.06 KB
/
jtool-ls
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
90
91
92
93
94
95
96
97
98
#!/bin/bash
# Usage: jtool-ls [OPTIONS] FILE...
########################################################################
# 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 (( $# == 1 )); then
case $1 in
-\?) exec ls --help ;;
-\:) 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 lps` output as a big string with spaces trimmed
(./"\n") as $lines
| $lines[0:-1][]
| (./" ") as $fields
| {
"mode": $fields[0],
"nlinks": $fields[1],
"user": $fields[2],
"group": $fields[3],
"size": $fields[4],
"date": $fields[5],
"time": $fields[6],
"TZ": $fields[7],
"name": $fields[8]
}
JQEOF
########################################################################
# Command
########################################################################
# `ls` options
declare -a LS_OPT=(
-l
--time-style=full-iso
)
# `jq` options
declare -a JQ_OPT=(
--slurp
--raw-input
--indent 4
--from-file ${JQ_SCRIPT}
)
# Filter to delete 1st line and trim spaces
declare -a SED_FILTER=(
-e '1d'
-e 's/[ \t]\+/ /g'
)
# `ls` with JSON output!
ls ${LS_OPT[@]} "$@" \
| sed "${SED_FILTER[@]}" \
| jq "${JQ_OPT[@]}"
########################################################################
# Bugs:
# Do not manage spaces in file names.
########################################################################
exit
# vim:syntax=sh:et:sw=4:ts=4:ai