From b10dba226142697e383ef5a02ca520d426d8c877 Mon Sep 17 00:00:00 2001 From: Colas Nahaboo Date: Fri, 4 Feb 2022 20:58:19 +0100 Subject: [PATCH] We now use set -xve instead of just xv in functions, to allow them to be used in scripts using set -e --- README.md | 4 ++-- src/regexp_nocase.sh | 6 +++--- src/regexp_quote.sh | 14 +++++++------- src/trim.sh | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 2774126..6fa9b71 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,9 @@ Most of the files in the form `src/foo.sh` have many **versions** of the same fu All these functions duplicate their code so that they are standalone and can be **copied individually**, unless clearly specified. It is an «anti-framework» approach. It is expected you will only copy the functions and the forms you are actually using into your scripts rather than loading the whole file as is (but it can be done). -The non-trivial functions start with a `local -; set +xv;` line that removes the **trace mode** when in the function. Thus when debugging a script by `set -xv` your log is not polluted by the internal tracing of these function that can be quite long when iterating on strings for instance. +The non-trivial functions start with a `local -; set +xve` line that removes the **trace mode** when in the function, with nearly no speed penalty. Thus when debugging a script by `set -xv` your log is not polluted by the internal tracing of these function that can be quite long when iterating on strings for instance. It also disables the `set -e` flag, just to be sure that these functions do not trigger spurious errors if you run your script with this flag. -The functions pass `shellcheck`, and can be used with `set -u`, but may not work with `set -e`. If you use `set -e`, just change the `set +xv` statement at the start of functions to `set +xve`. +The functions pass `shellcheck`, and can be used with `set -u`. ### library files diff --git a/src/regexp_nocase.sh b/src/regexp_nocase.sh index ae7b929..6cba20a 100644 --- a/src/regexp_nocase.sh +++ b/src/regexp_nocase.sh @@ -15,7 +15,7 @@ # They are pure bash, and the fastest code I could manage (no forks). # Also they turn of the debugging options (set -xv) during their execution # to avoid polluting your debug traces with their mundane code, by the -# statements "local -; set +xv;" at their start. +# statements "local -; set +xve;" at their start. # from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_nocase.sh @@ -31,7 +31,7 @@ # x[a-e[:space:][:upper:]]y ==> [xX][a-eA-E[:space:][:alpha:]][yY] # from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_nocase.sh v1 set_regexp_nocase(){ - local -; set +xv + local -; set +xve local -n _re="$1"; _re= local cre="$2" c chars ce prechar postchar notchar local -i i len=${#2} state=0 @@ -97,7 +97,7 @@ set_regexp_nocase(){ # E.g: rei=$(regexp_nocase "a*B"); echo "$rei" ==> "[aA]*[bB]" # from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_nocase.sh v1 regexp_nocase(){ - local -; set +xv + local -; set +xve local _re= local cre="$1" c chars ce prechar postchar notchar local -i i len=${#1} state=0 diff --git a/src/regexp_quote.sh b/src/regexp_quote.sh index ed7498a..b8ca358 100644 --- a/src/regexp_quote.sh +++ b/src/regexp_quote.sh @@ -18,7 +18,7 @@ # in bash scripts to differentiate them from the "regular" code. # Also they turn of the debugging options (set -xv) during their execution # to avoid polluting your debug traces with their mundane code, by the -# statements "local -; set +xv;" at their start. +# statements "local -; set +xve;" at their start. # from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_quote.sh @@ -31,7 +31,7 @@ # sets the variable in 1rst arg to the quoted 2nd arg string for regexp use, # quotes also / for safe use in sed /.../ # from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_quote.sh v1 -set_regexp_quote(){ local -; set +xv; local -n _qs="$1" +set_regexp_quote(){ local -; set +xve; local -n _qs="$1" local i c len="${#2}"; _qs=; for (( i=0; i