more jsonDB exmples with bash assoc array

This commit is contained in:
Kay Marquardt 2020-05-06 15:00:30 +02:00 committed by GitHub
parent a710e0cc33
commit fdc28e97f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -484,7 +484,7 @@ wWrite content of given ARRAY into file. ARRAY name must be delared with "decla
declare -A WRITEVALUES declare -A WRITEVALUES
WRITEVALUES["value1"]="example" WRITEVALUES["value1"]="example"
WRITEVALUES["value2"]="an other value" WRITEVALUES["value2"]="a value"
WRITEVALUES["whynot","subindex1"]="whynot A" WRITEVALUES["whynot","subindex1"]="whynot A"
WRITEVALUES["whynot","subindex2"]="whynot B" WRITEVALUES["whynot","subindex2"]="whynot B"
WRITEVALUES["whynot","subindex2","text"]="This is an example content for pseudo multidimensional bash array" WRITEVALUES["whynot","subindex2","text"]="This is an example content for pseudo multidimensional bash array"
@ -498,7 +498,7 @@ jssh_writeDB "WRITEVALUES" "${DATADIR:-}/myvalues"
# show whats written # show whats written
cat "${DATADIR:-}/myvalues.jssh" cat "${DATADIR:-}/myvalues.jssh"
["value1"] "example" ["value1"] "example"
["value2"] "an other value" ["value2"] "a value"
["whynot","subindex2","text"] "This is an example content for pseudo multidimensional bash array" ["whynot","subindex2","text"] "This is an example content for pseudo multidimensional bash array"
["whynot","subindex2"] "whynot B" ["whynot","subindex2"] "whynot B"
["whynot","subindex1"] "whynot A"``` ["whynot","subindex1"] "whynot A"```
@ -523,17 +523,42 @@ jssh_readDB "READVALUES" "${DATADIR:-}/myvalues"
# sinple command to output values ONLY # sinple command to output values ONLY
printf "${READVALUES[*]}" printf "${READVALUES[*]}"
# output key and value in a loop
for x in "${!READVALUES[@]}"; do printf "[%s]=%s\n" "$x" "${READVALUES[$x]}" ; done
[value1]=example # function to output key and value
[value2]=an other value printarr() { declare -n __p="$1"; for k in "${!__p[@]}"; do printf "%s=%s\n" "$k" "${__p[$k]}" ; done ; }
[whynot,subindex2,text]=This is an example content for pseudo multidimensional bash array
[whynot,subindex2]=whynot B printarr READVALUES
[whynot,subindex1]=whynot A value1=example
[whynot,subindex]=value3 value2=a value
whynot,subindex2,text=This is an example content for pseudo multidimensional bash array
whynot,subindex2=whynot B
whynot,subindex1=whynot A
# access Arrray
echo "${READVALUES[vaule2]}"
a value
# change / add values
READVALUES["value2"]="this is a changed value"
echo "${READVALUES[vaule2]}"
this is a changed value
READVALUES["value3"]="new value"
READVALUES[whynot,subindex3]="new subindex value"
# new output
printarr READVALUES
value1=example
value2=this is a changed value
whynot,subindex2,text=This is an example content for pseudo multidimensional bash array
whynot,subindex3=new subindex value
whynot,subindex2=whynot B
whynot,subindex1=whynot A
``` ```
https://linuxhint.com/associative_array_bash/
https://linuxconfig.org/how-to-use-arrays-in-bash-script
### Aliases - shortcuts for often used funtions ### Aliases - shortcuts for often used funtions