aboutsummaryrefslogtreecommitdiff
path: root/bin/edda
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2015-06-26 11:31:33 +1200
committerTom Ryder <tom@sanctum.geek.nz>2015-06-26 11:31:33 +1200
commitaffd67f4874cb252eb8271c60d0ebc81585b4e77 (patch)
treefdde241ee7d041ce60e72225776c23dac9b10535 /bin/edda
parentUndo hare-brained last few commits (diff)
downloaddotfiles-affd67f4874cb252eb8271c60d0ebc81585b4e77.tar.gz
dotfiles-affd67f4874cb252eb8271c60d0ebc81585b4e77.zip
Add edda(1)
Diffstat (limited to 'bin/edda')
-rwxr-xr-xbin/edda61
1 files changed, 61 insertions, 0 deletions
diff --git a/bin/edda b/bin/edda
new file mode 100755
index 00000000..154fb415
--- /dev/null
+++ b/bin/edda
@@ -0,0 +1,61 @@
+#!/usr/bin/env bash
+
+#
+# edda(1) -- Run ed(1) over multiple files, duplicating stdin. Example:
+#
+# $ edda /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"
+}
+
+# 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) || exit
+cleanup() {
+ rm -f "$stdin"
+}
+trap cleanup EXIT
+cat > "$stdin"
+
+# Need at least one file
+if ! (($#)) ; then
+ usage >&2
+ exit 1
+fi
+
+# Run ed(1) over each file with the options and stdin given
+for file ; do
+ ed "${opts[@]}" "$file" < "$stdin"
+done
+