aboutsummaryrefslogblamecommitdiff
path: root/bin/stbl
blob: 3bf0902ae7d4345a77d837e546d3b042fb5074d3 (plain) (tree)





































































                                                                         
#!/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