diff options
author | Einhard Leichtfuß <alguien@respiranto.de> | 2022-04-29 14:58:55 +0200 |
---|---|---|
committer | Einhard Leichtfuß <alguien@respiranto.de> | 2022-04-29 14:58:55 +0200 |
commit | 915faab9c074a0a30b8321c3836d0b35f475e94d (patch) | |
tree | 522b4109f2d43ff06db001b329ec1e09884cf6c8 /basic.bash | |
parent | 90a0069d9bb72bf3caf59e2411a144f9e3afd2d3 (diff) |
Use proper .bash suffix
Diffstat (limited to 'basic.bash')
-rw-r--r-- | basic.bash | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/basic.bash b/basic.bash new file mode 100644 index 0000000..643c39d --- /dev/null +++ b/basic.bash @@ -0,0 +1,102 @@ +#!/usr/bin/env bash +# +# Copyright 2018 Einhard Leichtfuß +# +# basic.bash - script containing the array of the to be generated dictionary's +# names and some common functionality, mostly wrapping sed. +# +# Copyright 2018,2022 Einhard Leichtfuß +# +# This file is part of aur-fd-scripts +# +# aur-fd-scripts is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# aur-fd-scripts is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with aur-fd-scripts. If not, see <https://www.gnu.org/licenses/>. +# + +# All methods operate on the file "$tmp". + + +script_path="$(realpath "$0")" +script_dir="${script_path%/*}" + +. "${script_dir}/general-config.bash" || exit 1 +. "${script_dir}/user-config.bash" || exit 1 + + +function init +{ + tmp_d="$(mktemp -d)" + tmp="$tmp_d/a" + tmp_="$tmp_d/b" +} + +# $1: file to treat as input: filename +# requires: init to be run +function input +{ + cp "$1" "$tmp" +} + +# $1: file to write to: filename +# requires: init and input to be run +function write +{ + cp "$tmp" "$1" +} + +function cleanup +{ + rm -rf "$tmp_d" +} +trap cleanup EXIT + +# $1: string to replace: extended regular expression +# $2: string to replace with: sed replacement string +function replace +{ + sed -Ei "s/${1}/${2}/g" "$tmp" +} + +# Replace line by a string (possibly including newlines). +# $1 line to replace: extended regular expression +# $2 text to replace with: (newline terminated) sed replacement string +# $3 removal type: (rm-plus-one|normal) +# `- If $2 is empty, whether to remove only that line or also the one +# following. +function replace_line +{ + if test -n "$2" + then + printf '%s' "$2" > "$tmp_" + + sed -Ei " + /${1}/ { + r $tmp_ + d + } + " "$tmp" + else + if [[ "$3" == rm-plus-one ]] + then + sed -Ei "/${1}/,+1 d" "$tmp" + else + sed -Ei "/${1}/ d" "$tmp" + fi + fi +} + +# $1 line to delete: extended regular expression +function delete_line +{ + sed -Ei "/${1}/ d" "$tmp" +} |