commit 6ece5330af02170401c2477fb89ea7208bd6ae83
parent 197aa2f9664635f798297681f60477f79d1b30b7
Author: Thien-Thi Nguyen <ttn@gnuvola.org>
Date: Sun, 24 Apr 2022 09:52:47 -0400
add support for optional arg SELEXP
* format.sh
(version): Bump to "1.3".
(selexp): Set to $2, if given.
<top-level>: Bifurcate on whether or not SELEXP is specified.
If specified, filter through ‘recsel -e SELEXP’ first.
Otherwise, run recfmt straight away, as before.
Diffstat:
1 file changed, 20 insertions(+), 3 deletions(-)
diff --git a/format.sh b/format.sh
@@ -1,13 +1,19 @@
#!/bin/sh
##
-# Usage: format.sh TEMPLATE
+# Usage: format.sh TEMPLATE [SELEXP]
#
# This runs recfmt w/ template file TEMPLATE, taking input from stdin,
# and writing output to stdout.
+#
+# Optional arg SELEXP is an expression passed to ‘recsel -e’. If specified,
+# stdin is first processed by recsel and its output is then piped to recfmt
+# for formatting. If recsel exits failurefully (e.g., given invalid SELEXP),
+# no output is written and format.sh exits failurefully as well.
##
me=$(basename $0)
-version='1.2'
+version='1.3'
+# 1.3 -- add support for optional arg SELEXP
# 1.2 -- add check for required arg TEMPLATE
# 1.1 -- add --help/--version support
# 1.0 -- initial release
@@ -29,6 +35,17 @@ fi
template="$1"
-exec recfmt -f "$template"
+if [ x"$2" = x ] ; then : ; else
+ selexp="$2"
+fi
+
+if [ "$selexp" ] ; then
+ t=$(mktemp -p .)
+ trap "rm -f $t" EXIT
+ recsel -e "$selexp" > $t &&
+ recfmt -f "$template" < $t
+else
+ exec recfmt -f "$template"
+fi
# format.sh ends here