From 670b632fd51bfbf4a003daefa4260e9c799c9ff8 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 27 Mar 2023 01:28:29 -0700 Subject: add `$opt=-' syntax for setting a config opt to an empty value `$opt=' was already being used for resetting an opt to its default value. --- backomp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/backomp b/backomp index aba6450..2aa873d 100755 --- a/backomp +++ b/backomp @@ -6,8 +6,8 @@ set -e function flr(n, s) { return int(n / s) * s } BEGIN { - split("h d w m", arr) - for (i = 1; i <= length(arr); ++i) { + len = split("h d w m", arr) + for (i = 1; i <= len; ++i) { b = arr[i] if (!match(retain, "([*0-9]+)" b, md)) @@ -64,8 +64,8 @@ run() { backup() { dest_dir="${1}" src_path="${2}" - retain="${3:-"*h*d*w*m"}" - replace="${4:-"h"}" + retain="${3}" + replace="${4}" [[ "$((dcount++))" -ne 0 ]] && echo echo "[${dcount}] [${retain}] ${src_path} => ${dest_dir}" @@ -96,15 +96,22 @@ backup() { printf 'kept %s\n' "${keep[@]}" } +declare -A def_opts=( + ['retain']='*h*d*w*m' + ['replace']='h' +) declare -A opts + while IFS='=' read -r f1 f2; do [[ -z "${f1}" ]] && continue if [[ "${f1}" =~ ^\[(.*)]$ ]]; then base_path="${BASH_REMATCH[1]}" - opts=() + opts="$(declare -p def_opts | sed '1s/[^=]*=//')" elif [[ "${f1}" == '$'* ]]; then - opts["${f1:1}"]="${f2}" + opt="${f1:1}" + [[ "${f2}" == '-' ]] && opts["${opt}"]= || + opts["${opt}"]="${f2:-"${def_opts["${opt}"]}"}" else backup "${base_path}/${f1}" "${f2}" \ "${opts['retain']}" "${opts['replace']}" -- cgit v1.2.3-70-g09d2