Skip to content

Commit b10dba2

Browse files
committed
We now use set -xve instead of just xv in functions, to allow them to be used in scripts using set -e
1 parent 6d64e94 commit b10dba2

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ Most of the files in the form `src/foo.sh` have many **versions** of the same fu
2424

2525
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).
2626

27-
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.
27+
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.
2828

29-
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`.
29+
The functions pass `shellcheck`, and can be used with `set -u`.
3030

3131
### library files
3232

src/regexp_nocase.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# They are pure bash, and the fastest code I could manage (no forks).
1616
# Also they turn of the debugging options (set -xv) during their execution
1717
# to avoid polluting your debug traces with their mundane code, by the
18-
# statements "local -; set +xv;" at their start.
18+
# statements "local -; set +xve;" at their start.
1919

2020
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_nocase.sh
2121

@@ -31,7 +31,7 @@
3131
# x[a-e[:space:][:upper:]]y ==> [xX][a-eA-E[:space:][:alpha:]][yY]
3232
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_nocase.sh v1
3333
set_regexp_nocase(){
34-
local -; set +xv
34+
local -; set +xve
3535
local -n _re="$1"; _re=
3636
local cre="$2" c chars ce prechar postchar notchar
3737
local -i i len=${#2} state=0
@@ -97,7 +97,7 @@ set_regexp_nocase(){
9797
# E.g: rei=$(regexp_nocase "a*B"); echo "$rei" ==> "[aA]*[bB]"
9898
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_nocase.sh v1
9999
regexp_nocase(){
100-
local -; set +xv
100+
local -; set +xve
101101
local _re=
102102
local cre="$1" c chars ce prechar postchar notchar
103103
local -i i len=${#1} state=0

src/regexp_quote.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# in bash scripts to differentiate them from the "regular" code.
1919
# Also they turn of the debugging options (set -xv) during their execution
2020
# to avoid polluting your debug traces with their mundane code, by the
21-
# statements "local -; set +xv;" at their start.
21+
# statements "local -; set +xve;" at their start.
2222

2323
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_quote.sh
2424

@@ -31,7 +31,7 @@
3131
# sets the variable in 1rst arg to the quoted 2nd arg string for regexp use,
3232
# quotes also / for safe use in sed /.../
3333
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_quote.sh v1
34-
set_regexp_quote(){ local -; set +xv; local -n _qs="$1"
34+
set_regexp_quote(){ local -; set +xve; local -n _qs="$1"
3535
local i c len="${#2}"; _qs=; for (( i=0; i<len; i++ ));do
3636
c="${2:i:1}";case "$c" in [^][{}?*\|+.$\\\(\)/^])_qs+="$c";;'^')_qs+='\^';;
3737
*)_qs+="[$c]";;esac;done
@@ -40,7 +40,7 @@ set_regexp_quote(){ local -; set +xv; local -n _qs="$1"
4040
# quote the 2nd arg string for regexp use, in a case insensitive way
4141
# sets the results into the variable first argument
4242
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_quote.sh v1
43-
set_regexp_quote_nocase(){ local -; set +xv; local -n _qs="$1"
43+
set_regexp_quote_nocase(){ local -; set +xve; local -n _qs="$1"
4444
local i c len="${#2}"; _qs=; for (( i=0; i<len; i++ ));do
4545
c="${2:i:1}";case "$c" in [[:alpha:]])_qs+="[${c,}${c^}]";;
4646
[^][{}?*\|+.$\\\(\)/^])_qs+="$c";;'^')_qs+='\^';;*)_qs+="[$c]";;
@@ -50,7 +50,7 @@ set_regexp_quote_nocase(){ local -; set +xv; local -n _qs="$1"
5050
# appends to the variable in 1rst arg to the quoted 2nd arg for regexp use,
5151
# quotes also / for safe use in sed /.../
5252
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_quote.sh v1
53-
add_regexp_quote(){ local -; set +xv; local -n _qs="$1"
53+
add_regexp_quote(){ local -; set +xve; local -n _qs="$1"
5454
local i c len="${#2}";for (( i=0; i<len; i++ ));do
5555
c="${2:i:1}";case "$c" in [^][{}?*\|+.$\\\(\)/^])_qs+="$c";;'^')_qs+='\^';;
5656
*)_qs+="[$c]";;esac;done
@@ -59,7 +59,7 @@ add_regexp_quote(){ local -; set +xv; local -n _qs="$1"
5959
# quote the 2nd arg string for regexp use, in a case insensitive way
6060
# appends the results into first argument
6161
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_quote.sh v1
62-
add_regexp_quote_nocase(){ local -; set +xv; local -n _qs="$1"
62+
add_regexp_quote_nocase(){ local -; set +xve; local -n _qs="$1"
6363
local i c len="${#2}"; for (( i=0; i<len; i++ ));do
6464
c="${2:i:1}";case "$c" in [[:alpha:]])_qs+="[${c,}${c^}]";;
6565
[^][{}?*\|+.$\\\(\)/^])_qs+="$c";;'^')_qs+='\^';;*)_qs+="[$c]";;
@@ -74,15 +74,15 @@ add_regexp_quote_nocase(){ local -; set +xv; local -n _qs="$1"
7474
# quote the argument string for regexp use, and prints them on stdout
7575
# quotes also / for safe use in sed /.../
7676
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_quote.sh v1
77-
regexp_quote(){ local -; set +xv
77+
regexp_quote(){ local -; set +xve
7878
local i c len="${#1}" r; for (( i=0; i<len; i++ ));do
7979
c="${1:i:1}";case "$c" in [^][{}?*\|+.$\\\(\)/^])r+="$c";;'^')r+='\^';;
8080
*)r+="[$c]";;esac;done; echo "$r"
8181
}
8282

8383
# quote the arg string for regexp use, case insensitive version
8484
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_quote.sh v1
85-
regexp_quote_nocase(){ local -; set +xv
85+
regexp_quote_nocase(){ local -; set +xve
8686
local i r c len="${#1}";for (( i=0; i<len; i++ ));do
8787
c="${1:i:1}";case "$c" in [[:alpha:]])r+="[${c,}${c^}]";;
8888
[^][{}?*\|+.$\\\(\)/^])r+="$c";;'^')r+='\^';;*)r+="[$c]";;

src/trim.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# They are pure bash, and the fastest code I could manage (no forks).
1717
# Also they turn of the debugging options (set -xv) during their execution
1818
# to avoid polluting your debug traces with their mundane code, by the
19-
# statements "local -; set +xv;" at their start.
19+
# statements "local -; set +xve;" at their start.
2020

2121
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/trim.sh
2222

@@ -30,7 +30,7 @@
3030
# from start and end
3131
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/trim.sh v1
3232
set_trim(){
33-
local -; set +xv
33+
local -; set +xve
3434
local -n _v="$1"
3535
_v="$2"
3636
_v="${2#"${_v%%[![:space:]]*}"}"
@@ -44,7 +44,7 @@ set_trim(){
4444
# variable argument
4545
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/trim.sh v1
4646
var_trim(){
47-
local -; set +xv
47+
local -; set +xve
4848
local -n _v="$1"
4949
_v="${_v#"${_v%%[![:space:]]*}"}"
5050
_v="${_v%"${_v##*[![:space:]]}"}"
@@ -56,7 +56,7 @@ var_trim(){
5656
# removes the spaces from start and end of the string argument
5757
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/trim.sh v1
5858
trim(){
59-
local -; set +xv
59+
local -; set +xve
6060
local v="$1"
6161
v="${v#"${v%%[![:space:]]*}"}"
6262
v="${v%"${v##*[![:space:]]}"}"

0 commit comments

Comments
 (0)