Skip to content

Commit ac1a3ca

Browse files
committed
Delete temp files
1 parent b937740 commit ac1a3ca

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

JSONPath.sh

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ JSON=0
1717
PRINT=1
1818
MULTIPASS=0
1919
FLATTEN=0
20+
STDINFILE=/var/tmp/JSONPath.$$.stdin
21+
STDINFILE2=/var/tmp/JSONPath.$$.stdin2
22+
PASSFILE=/var/tmp/JSONPath.$$.pass1
2023
declare -a INDEXMATCH_QUERY
2124

2225
# ---------------------------------------------------------------------------
@@ -26,13 +29,13 @@ main() {
2629

2730
parse_options "$@"
2831

32+
trap cleanup EXIT
33+
2934
if [[ $QUERY == *'?(@'* ]]; then
3035
# This will be a multipass query
3136

32-
STDINFILE=/var/tmp/JSONPath.$$.stdin
3337
[[ -n $FILE ]] && STDINFILE=$FILE
3438
[[ -z $FILE ]] && cat >$STDINFILE
35-
PASSFILE=/var/tmp/JSONPath.$$.pass1
3639

3740
while true; do
3841
tokenize_path
@@ -55,8 +58,6 @@ main() {
5558
break
5659
done
5760

58-
[[ -z $FILE ]] && rm -f $STDINFILE
59-
rm -f $PASSFILE
6061
else
6162

6263
tokenize_path
@@ -88,6 +89,15 @@ reset() {
8889
MULTIPASS=0
8990
}
9091

92+
# ---------------------------------------------------------------------------
93+
cleanup() {
94+
# ---------------------------------------------------------------------------
95+
96+
[[ -e "$PASSFILE" ]] && rm -f "$PASSFILE"
97+
[[ -e "$STDINFILE2" ]] && rm -f "$STDINFILE2"
98+
[[ -z "$FILE" && -e "$STDINFILE" ]] && rm -f "$STDINFILE"
99+
}
100+
91101
# ---------------------------------------------------------------------------
92102
usage() {
93103
# ---------------------------------------------------------------------------
@@ -491,8 +501,7 @@ flatten() {
491501
local path a prevpath pathlen
492502

493503
if [[ $FLATTEN -eq 1 ]]; then
494-
STDINFILE=/var/tmp/JSONPath.$$.stdin
495-
cat >"$STDINFILE"
504+
cat >"$STDINFILE2"
496505

497506
highest=9999
498507

@@ -517,13 +526,13 @@ flatten() {
517526
[[ $high -lt $highest ]] && highest=$high
518527

519528
prevpath=("${path[@]}")
520-
done <"$STDINFILE"
529+
done <"$STDINFILE2"
521530

522531
if [[ $highest -gt 0 ]]; then
523532
sed -r 's/\[(([0-9]+|"[^"]+")[],]){'$((highest))'}(.*)/[\3/' \
524-
"$STDINFILE"
533+
"$STDINFILE2"
525534
else
526-
cat "$STDINFILE"
535+
cat "$STDINFILE2"
527536
fi
528537
else
529538
cat

0 commit comments

Comments
 (0)