#!/bin/bash # # Library for managing files # Functions ######################## # Replace a regex in a file # Arguments: # $1 - filename # $2 - match regex # $3 - substitute regex # $4 - use POSIX regex. Default: true # Returns: # None ######################### replace_in_file() { local filename="${1:?filename is required}" local match_regex="${2:?match regex is required}" local substitute_regex="${3:?substitute regex is required}" local posix_regex=${4:-true} local result # We should avoid using 'sed in-place' substitutions # 1) They are not compatible with files mounted from ConfigMap(s) # 2) We found incompatibility issues with Debian10 and "in-place" substitutions if [[ $posix_regex = true ]]; then result="$(sed -E "s@$match_regex@$substitute_regex@g" "$filename")" else result="$(sed "s@$match_regex@$substitute_regex@g" "$filename")" fi echo "$result" > "$filename" } ######################## # Remove a line in a file based on a regex # Arguments: # $1 - filename # $2 - match regex # $3 - use POSIX regex. Default: true # Returns: # None ######################### remove_in_file() { local filename="${1:?filename is required}" local match_regex="${2:?match regex is required}" local posix_regex=${3:-true} local result # We should avoid using 'sed in-place' substitutions # 1) They are not compatible with files mounted from ConfigMap(s) # 2) We found incompatibility issues with Debian10 and "in-place" substitutions if [[ $posix_regex = true ]]; then result="$(sed -E "/$match_regex/d" "$filename")" else result="$(sed "/$match_regex/d" "$filename")" fi echo "$result" > "$filename" }