aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-03-28 15:37:37 +1300
committerTom Ryder <tom@sanctum.geek.nz>2016-03-28 15:37:37 +1300
commitd8590a2296f3454143dca806d30db2f8ac5172f6 (patch)
tree22d4288b40f8d11046142535405949d27fb33754 /bin
parentCorrect incorrect stream in comment (diff)
downloaddotfiles-d8590a2296f3454143dca806d30db2f8ac5172f6.tar.gz
dotfiles-d8590a2296f3454143dca806d30db2f8ac5172f6.zip
Add stbl(1)
Diffstat (limited to 'bin')
-rwxr-xr-xbin/stbl70
1 files changed, 70 insertions, 0 deletions
diff --git a/bin/stbl b/bin/stbl
new file mode 100755
index 00000000..3bf0902a
--- /dev/null
+++ b/bin/stbl
@@ -0,0 +1,70 @@
+#!/usr/bin/env bash
+
+#
+# stbl(1) - Strip a trailing blank line from the given files with ed(1).
+#
+# -h gives help, -v prints the names of the files on stderr as they're
+# processed.
+#
+# Author: Tom Ryder <tom@sanctum.geek.nz>
+# Copyright: 2016
+# License: Public domain
+#
+self=stbl
+
+# Print usage information
+usage() {
+ printf '%s: usage: %s [-hv] [--] FILE1 [FILE2...]\n' \
+ "$self" "$self"
+}
+
+# Flag for whether to print diagnostics to stderr or not; defaults to off
+declare -i verbose
+verbose=0
+
+# Process options
+while getopts 'hv' opt ; do
+ case $opt in
+
+ # -h: Print help
+ h)
+ usage
+ exit 0
+ ;;
+
+ # -v: Print diagnostics to stderr
+ v)
+ verbose=1
+ ;;
+
+ # Unknown option
+ \?)
+ usage >&2
+ exit 2
+ ;;
+ esac
+done
+shift "$((OPTIND-1))"
+
+# Check we have arguments left
+if ! (($#)) ; then
+ usage >&2
+ exit 2
+fi
+
+# Check we have ed(1)
+hash ed || exit
+
+# Iterate through the arguments
+for fn ; do
+
+ # If verbose is set, print what we're doing
+ ((verbose)) && printf '%s: %s\n' \
+ "$self" "$fn" >&2
+
+ # Run the ed script to strip the trailing blank lines
+ ed -s -- "$fn" <<'EOF'
+$g/^ *$/d
+w
+EOF
+done