diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-03-28 15:37:37 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-03-28 15:37:37 +1300 |
commit | d8590a2296f3454143dca806d30db2f8ac5172f6 (patch) | |
tree | 22d4288b40f8d11046142535405949d27fb33754 | |
parent | Correct incorrect stream in comment (diff) | |
download | dotfiles-d8590a2296f3454143dca806d30db2f8ac5172f6.tar.gz dotfiles-d8590a2296f3454143dca806d30db2f8ac5172f6.zip |
Add stbl(1)
-rw-r--r-- | README.markdown | 1 | ||||
-rwxr-xr-x | bin/stbl | 70 | ||||
-rw-r--r-- | man/man1/stbl.1 | 15 |
3 files changed, 86 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown index ff7434f4..3f97c48b 100644 --- a/README.markdown +++ b/README.markdown @@ -284,6 +284,7 @@ Scripts that don’t actually worry you, exiting with 0 anyway. * `maybe(1)` is like `true(1)` or `false(1)`; given a probability of success, it exits with success or failure. Good for quick tests. +* `stbl(1)` strips a trailing blank line from the files in its arguments. * `sue(8)` execs `sudoedit(8)` as the owner of all the file arguments given, perhaps in cases where you may not necessarily have `root` `sudo(8)` privileges. 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 diff --git a/man/man1/stbl.1 b/man/man1/stbl.1 new file mode 100644 index 00000000..1462f80f --- /dev/null +++ b/man/man1/stbl.1 @@ -0,0 +1,15 @@ +.TH STBL 1 "March 2016" "Manual page for stbl" +.SH NAME +.B stbl +\- strip a trailing blank line from a file +.SH USAGE +.B stbl [-hv] FILE1 [FILE2 ...] +.SH DESCRIPTION +Applies ed(1) to remove a trailing newline from each of the given files. +.P +Option -h gives help, option -v turns on verbose output. +.P + $ stbl .bashrc + $ stbl -v project/*.c project/*.h +.SH AUTHOR +Tom Ryder <tom@sanctum.geek.nz> |