Skip to content

Commit

Permalink
We now use set -xve instead of just xv in functions, to allow them to…
Browse files Browse the repository at this point in the history
… be used in scripts using set -e
  • Loading branch information
ColasNahaboo committed Feb 4, 2022
1 parent 6d64e94 commit b10dba2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions src/regexp_nocase.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/regexp_quote.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<len; i++ ));do
c="${2:i:1}";case "$c" in [^][{}?*\|+.$\\\(\)/^])_qs+="$c";;'^')_qs+='\^';;
*)_qs+="[$c]";;esac;done
Expand All @@ -40,7 +40,7 @@ set_regexp_quote(){ local -; set +xv; local -n _qs="$1"
# quote the 2nd arg string for regexp use, in a case insensitive way
# sets the results into the variable first argument
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_quote.sh v1
set_regexp_quote_nocase(){ local -; set +xv; local -n _qs="$1"
set_regexp_quote_nocase(){ local -; set +xve; local -n _qs="$1"
local i c len="${#2}"; _qs=; for (( i=0; i<len; i++ ));do
c="${2:i:1}";case "$c" in [[:alpha:]])_qs+="[${c,}${c^}]";;
[^][{}?*\|+.$\\\(\)/^])_qs+="$c";;'^')_qs+='\^';;*)_qs+="[$c]";;
Expand All @@ -50,7 +50,7 @@ set_regexp_quote_nocase(){ local -; set +xv; local -n _qs="$1"
# appends to the variable in 1rst arg to the quoted 2nd arg 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
add_regexp_quote(){ local -; set +xv; local -n _qs="$1"
add_regexp_quote(){ local -; set +xve; local -n _qs="$1"
local i c len="${#2}";for (( i=0; i<len; i++ ));do
c="${2:i:1}";case "$c" in [^][{}?*\|+.$\\\(\)/^])_qs+="$c";;'^')_qs+='\^';;
*)_qs+="[$c]";;esac;done
Expand All @@ -59,7 +59,7 @@ add_regexp_quote(){ local -; set +xv; local -n _qs="$1"
# quote the 2nd arg string for regexp use, in a case insensitive way
# appends the results into first argument
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_quote.sh v1
add_regexp_quote_nocase(){ local -; set +xv; local -n _qs="$1"
add_regexp_quote_nocase(){ local -; set +xve; local -n _qs="$1"
local i c len="${#2}"; for (( i=0; i<len; i++ ));do
c="${2:i:1}";case "$c" in [[:alpha:]])_qs+="[${c,}${c^}]";;
[^][{}?*\|+.$\\\(\)/^])_qs+="$c";;'^')_qs+='\^';;*)_qs+="[$c]";;
Expand All @@ -74,15 +74,15 @@ add_regexp_quote_nocase(){ local -; set +xv; local -n _qs="$1"
# quote the argument string for regexp use, and prints them on stdout
# quotes also / for safe use in sed /.../
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_quote.sh v1
regexp_quote(){ local -; set +xv
regexp_quote(){ local -; set +xve
local i c len="${#1}" r; for (( i=0; i<len; i++ ));do
c="${1:i:1}";case "$c" in [^][{}?*\|+.$\\\(\)/^])r+="$c";;'^')r+='\^';;
*)r+="[$c]";;esac;done; echo "$r"
}

# quote the arg string for regexp use, case insensitive version
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/regexp_quote.sh v1
regexp_quote_nocase(){ local -; set +xv
regexp_quote_nocase(){ local -; set +xve
local i r c len="${#1}";for (( i=0; i<len; i++ ));do
c="${1:i:1}";case "$c" in [[:alpha:]])r+="[${c,}${c^}]";;
[^][{}?*\|+.$\\\(\)/^])r+="$c";;'^')r+='\^';;*)r+="[$c]";;
Expand Down
8 changes: 4 additions & 4 deletions src/trim.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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/trim.sh

Expand All @@ -30,7 +30,7 @@
# from start and end
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/trim.sh v1
set_trim(){
local -; set +xv
local -; set +xve
local -n _v="$1"
_v="$2"
_v="${2#"${_v%%[![:space:]]*}"}"
Expand All @@ -44,7 +44,7 @@ set_trim(){
# variable argument
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/trim.sh v1
var_trim(){
local -; set +xv
local -; set +xve
local -n _v="$1"
_v="${_v#"${_v%%[![:space:]]*}"}"
_v="${_v%"${_v##*[![:space:]]}"}"
Expand All @@ -56,7 +56,7 @@ var_trim(){
# removes the spaces from start and end of the string argument
# from: https://github.com/ColasNahaboo/colas-bash-lib/blob/main/lib/trim.sh v1
trim(){
local -; set +xv
local -; set +xve
local v="$1"
v="${v#"${v%%[![:space:]]*}"}"
v="${v%"${v##*[![:space:]]}"}"
Expand Down

0 comments on commit b10dba2

Please sign in to comment.