]> ruderich.org/simon Gitweb - config/dotfiles.git/commitdiff
lib.sh: Generalize sed_i() to cmd_i() for in-place changes.
authorSimon Ruderich <simon@ruderich.org>
Tue, 22 Oct 2013 19:15:31 +0000 (21:15 +0200)
committerSimon Ruderich <simon@ruderich.org>
Tue, 22 Oct 2013 19:15:31 +0000 (21:15 +0200)
lib.sh

diff --git a/lib.sh b/lib.sh
index 7b76a7cab66b08cad1d639f0c059fae2dc637798..5abd27898400e44bbaed2c14ec4a45ce80b8f480 100644 (file)
--- a/lib.sh
+++ b/lib.sh
@@ -50,21 +50,29 @@ installed_path() {
     )
 }
 
     )
 }
 
-# Usage: sed_i ... <file>
+# Usage: cmd_i <cmd> ... <file>
 #
 #
-# Uses <file>.tmp as temporary file. sed -i is not compatible due to different
-# implementations.
-sed_i() {
+# Run <cmd> with all arguments (including the last file) and write the result
+# to the temporary file <file>.tmp and then renamed that file to <file>. This
+# can't be done in-place (e.g. cmd <file >file) because it truncates the file.
+cmd_i() {
     # Get last argument.
     last=
     for x; do
         last="$x"
     done
 
     # Get last argument.
     last=
     for x; do
         last="$x"
     done
 
-    sed "$@" >"$last".tmp
+    "$@" >"$last".tmp
     mv "$last".tmp "$last"
 }
 
     mv "$last".tmp "$last"
 }
 
+# Usage: sed_i ... <file>
+#
+# sed -i is not compatible due to different implementations. See cmd_i.
+sed_i() {
+    cmd_i sed "$@"
+}
+
 # Print the current OS. The following OS are supported at the moment:
 # - Debian (debian)
 # - Gentoo (gentoo)
 # Print the current OS. The following OS are supported at the moment:
 # - Debian (debian)
 # - Gentoo (gentoo)