aboutsummaryrefslogtreecommitdiff
path: root/bin/edda
blob: 6af88a5f91bb9fb5ff37798a087aa4e79513ca3a (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
#!/bin/sh
# Run ed(1) over multiple files, duplicating stdin.
self=edda

# Need at least one file
if [ "$#" -eq 0 ] ; then
    printf >&2 'edda: Need at least one file\n'
    exit 2
fi

# Create a temporary directory with name in $td, and handle POSIX-ish traps to
# remove it when the script exits.
td=
cleanup() {
    [ -n "$td" ] && rm -fr -- "$td"
    if [ "$1" != EXIT ] ; then
        trap - "$1"
        kill "-$1" "$$"
    fi
}
for sig in EXIT HUP INT TERM ; do
    # shellcheck disable=SC2064
    trap "cleanup $sig" "$sig"
done
td=$(mktd "$self") || exit

# Duplicate stdin into a file
script=$td/script
cat >"$script" || exit

# Run ed(1) over each file with the stdin given
for file ; do
    ed -- "$file" <"$script"
done