aboutsummaryrefslogtreecommitdiff
path: root/bin/edda
blob: 26d78b94857c51cb0b4319b435c535658765df0d (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
#!/usr/bin/env bash
# Run ed(1) over multiple files, duplicating stdin.

# Give up completely if no BASH_VERSINFO (<2.0)
[ -n "$BASH_VERSINFO" ] || exit

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

# Need at least one file after options are parsed out
if ! (($#)) ; then
    printf >&2 'edda: Need at least one file\n'
    exit 2
fi

# Create a temporary directory with name in $td, and a trap to remove it when
# the script exits
td=
cleanup() {
    [[ -n "$td" ]] && rm -fr -- "$td"
}
trap cleanup EXIT
td=$(mktd "$self") || exit

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

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