From d3ebc21eadc5c32edc1ffaa026f51c260c2536fe Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 21 Sep 2016 19:11:48 +0300 Subject: add replace_word, swap_files --- .bash_utils/file.sh | 35 +++++++++++++++++++++++++++++++++++ .bash_utils/text.sh | 14 ++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 .bash_utils/file.sh diff --git a/.bash_utils/file.sh b/.bash_utils/file.sh new file mode 100644 index 0000000..0321833 --- /dev/null +++ b/.bash_utils/file.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +# Copyright (c) 2016 Egor Tensin +# This file is part of the "Cygwin configuration files" project. +# For details, see https://github.com/egor-tensin/cygwin-home. +# Distributed under the MIT License. + +swap_files() ( + set -o errexit -o nounset -o pipefail + + if [ "$#" -ne 2 ]; then + echo "usage: ${FUNCNAME[0]} PATH1 PATH2" >&2 + return 1 + fi + + local path1="$1" + local path2="$2" + + if [ ! -f "$path1" ]; then + echo "${FUNCNAME[0]}: must be a regular file: $path1" >&2 + return 1 + fi + + if [ ! -f "$path2" ]; then + echo "${FUNCNAME[0]}: must be a regular file: $path2" >&2 + return 1 + fi + + local tmp_path + tmp_path="$( mktemp "$( dirname "$path1" )/XXX" )" + + mv "$path1" "$tmp_path" + mv "$path2" "$path1" + mv "$tmp_path" "$path2" +) diff --git a/.bash_utils/text.sh b/.bash_utils/text.sh index 5a678ee..cb34073 100644 --- a/.bash_utils/text.sh +++ b/.bash_utils/text.sh @@ -25,3 +25,17 @@ lint() { doslint() { trim "$@" && trimdoseol "$@" && doseol "$@" } + +replace_word() ( + set -o errexit -o nounset -o pipefail + + if [ "$#" -lt 3 ]; then + echo "usage: ${FUNCNAME[0]} OLD NEW PATH [PATH...]" >&2 + return 1 + fi + + local old="$1" + local new="$2" + + shift && shift && sed --binary --in-place "s/\\b$old\\b/$new/g" "$@" +) -- cgit v1.2.3