aboutsummaryrefslogtreecommitdiff
path: root/bin/edda
blob: 6e290cc5e44251cbf7ce5135cd983fc48b5ab374 (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
#!/usr/bin/env bash

#
# edda(1) -- Run ed(1) over multiple files, duplicating stdin. Example:
#
#   $ edda -s /etc/app.d/*.conf <<EOF
#   ,s/foo/bar/g
#   w
#   EOF
#
# Author: Tom Ryder <tom@sanctum.geek.nz>
# Copyright: 2015
# License: Public domain
#

# Name self
self=edda

# Define usage function
usage() {
    printf 'USAGE: %s [OPTS] [--] FILE1 [FILE2...]\n' "$self"
}

# Need at least one file
if ! (($#)) ; then
    usage >&2
    exit 1
fi

# Need ed(1) -- some systems daring to call themselves UNIX-like don't have it
# installed by default. What's POSIX, precious?
hash ed || exit

# Parse options out, give help if necessary
declare -a opts
for arg ; do
    case $arg in
        --help|-h|-\?)
            usage
            exit
            ;;
        --)
            shift
            break
            ;;
        -*)
            shift
            opts[${#opts[@]}]=$arg
            ;;
    esac
done

# Duplicate stdin into a file, which we'll remove on exit
stdin=$(mktemp -dt "$self".XXXXXX) || exit
cleanup() {
    rm -f -- "$stdin"
}
trap cleanup EXIT
cat > "$stdin"

# Run ed(1) over each file with the options and stdin given
for file ; do
    ed "${opts[@]}" -- "$file" < "$stdin"
done