aboutsummaryrefslogtreecommitdiff
path: root/bin/stbl
blob: 3bf0902ae7d4345a77d837e546d3b042fb5074d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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