From: Simon Ruderich Date: Tue, 22 Oct 2013 19:15:31 +0000 (+0200) Subject: lib.sh: Generalize sed_i() to cmd_i() for in-place changes. X-Git-Url: https://ruderich.org/simon/gitweb/?p=config%2Fdotfiles.git;a=commitdiff_plain;h=8348ecd5e6759f9551fac0431a45c3bdffc36956 lib.sh: Generalize sed_i() to cmd_i() for in-place changes. --- diff --git a/lib.sh b/lib.sh index 7b76a7c..5abd278 100644 --- a/lib.sh +++ b/lib.sh @@ -50,21 +50,29 @@ installed_path() { ) } -# Usage: sed_i ... +# Usage: cmd_i ... # -# Uses .tmp as temporary file. sed -i is not compatible due to different -# implementations. -sed_i() { +# Run with all arguments (including the last file) and write the result +# to the temporary file .tmp and then renamed that file to . This +# can't be done in-place (e.g. cmd file) because it truncates the file. +cmd_i() { # Get last argument. last= for x; do last="$x" done - sed "$@" >"$last".tmp + "$@" >"$last".tmp mv "$last".tmp "$last" } +# Usage: sed_i ... +# +# 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)