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
1 changed files with 36 additions and 11 deletions

View File

@ -484,7 +484,7 @@ wWrite content of given ARRAY into file. ARRAY name must be delared with "decla
declare -A WRITEVALUES
WRITEVALUES["value1"]="example"
WRITEVALUES["value2"]="an other value"
WRITEVALUES["value2"]="a value"
WRITEVALUES["whynot","subindex1"]="whynot A"
WRITEVALUES["whynot","subindex2"]="whynot B"
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
cat "${DATADIR:-}/myvalues.jssh"
["value1"] "example"
["value2"] "an other value"
["value2"] "a value"
["whynot","subindex2","text"] "This is an example content for pseudo multidimensional bash array"
["whynot","subindex2"] "whynot B"
["whynot","subindex1"] "whynot A"```
@ -523,17 +523,42 @@ jssh_readDB "READVALUES" "${DATADIR:-}/myvalues"
# sinple command to output values ONLY
printf "${READVALUES[*]}"
# output key and value in a loop
for x in "${!READVALUES[@]}"; do printf "[%s]=%s\n" "$x" "${READVALUES[$x]}" ; done
[value1]=example
[value2]=an other value
[whynot,subindex2,text]=This is an example content for pseudo multidimensional bash array
[whynot,subindex2]=whynot B
[whynot,subindex1]=whynot A
[whynot,subindex]=value3
# function to output key and value
printarr() { declare -n __p="$1"; for k in "${!__p[@]}"; do printf "%s=%s\n" "$k" "${__p[$k]}" ; done ; }
printarr READVALUES
value1=example
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