]> ruderich.org/simon Gitweb - config/dotfiles.git/blobdiff - lib.sh
lib.sh: Generalize sed_i() to cmd_i() for in-place changes.
[config/dotfiles.git] / lib.sh
diff --git a/lib.sh b/lib.sh
index 204a8e9f95a2e695c4822682f85de7bf6cb1727c..5abd27898400e44bbaed2c14ec4a45ce80b8f480 100644 (file)
--- a/lib.sh
+++ b/lib.sh
@@ -50,6 +50,29 @@ installed_path() {
     )
 }
 
+# Usage: cmd_i <cmd> ... <file>
+#
+# 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
+
+    "$@" >"$last".tmp
+    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)