doc: modules: multiple manipulation without tmp var

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2021-01-06 19:22:24 +01:00
parent efca2e013b
commit 6f8515d701
2 changed files with 20 additions and 6 deletions

View File

@ -262,6 +262,20 @@ VAR="$(( 1 + 2 ))" -> (( var=1+2 ))
INDEX="$(( ${INDEX} + 1 ))" -> (( INDEX++ ))
# manipulate the same variable multiple times without storing intermediate
START="1a23_b__x###"
BAR="${START//[0-9]}" # a_b__x###
BAR="${BAR%%#*}" # a_b__x
BAR="${BAR/__x/_c}" # a_b_c
: "${START//[0-9]}" # a_b__x###
: "${_%%#*}" # a_b__x
BAR="${_/__x/_c}" # a_b_c
# works also for output of commands
: "$(date "+%A")" # Weekday
UPPERDAY="${_^^}" # WEEKDAY
```
For more examples see [Pure bash bible](https://github.com/dylanaraps/pure-bash-bible)
@ -358,5 +372,5 @@ fi
#### [Prev Function Reference](6_reference.md)
#### $$VERSION$$ v1.25-dev-23-g8be95a3
#### $$VERSION$$ v1.25-dev-30-gefca2e0

View File

@ -5,7 +5,7 @@
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#
#### $$VERSION$$ v1.25-dev-14-g2fe6d4b
#### $$VERSION$$ v1.25-dev-30-gefca2e0
#
# source from commands.sh to use jsonDB functions
#
@ -362,13 +362,13 @@ Json2Array() {
# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling
Array2Json() {
[ -z "$1" ] && return 1
local key val
local key
declare -n ARRAY="$1"
for key in "${!ARRAY[@]}"
do
[[ "${key}" =~ ^${JSSH_KEYOK}+$ ]] || continue
# in case val contains newline convert to \n
val="${ARRAY[${key}]//$'\n'/\\n}"
printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${val//\"/\\\"}"
# in case value contains newline convert to \n
: "${ARRAY[${key}]//$'\n'/\\n}"
printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${_//\"/\\\"}"
done
}