mirror of
https://github.com/octoleo/telegram-bot-bash.git
synced 2024-11-25 16:57:33 +00:00
doc: JSSHDB_UNSET value
This commit is contained in:
parent
192fae8a37
commit
1a0642bcce
@ -1042,9 +1042,14 @@ ARRAY["key"]="value"
|
|||||||
ARRAY["key,subkey"]="value2"
|
ARRAY["key,subkey"]="value2"
|
||||||
```
|
```
|
||||||
|
|
||||||
For keys the following charatcsers are allowed: `a-z A-Z 0-9 _ .`, multiple keys must be separated by `,`.
|
Only the following characters are allowed: `a-z A-Z 0-9 _ .` for keys, multiple keys must be separated by `,`.
|
||||||
Keys contaiing other characters will be discarded when written to a file.
|
Keys contaiing other characters will be discarded when written to a file.
|
||||||
|
|
||||||
|
To delete (unset) a key/value pair in memory you can `unset ARRAY["abc"]` but this will not delete key/value
|
||||||
|
piars when using `jssh_updateDB`. Therefore the special value `${JSSHDB_UNSET}` exists to delete a key/value pair
|
||||||
|
when updateting a file, see `jssh_updateDB`
|
||||||
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
ARRAY["abc"]="abc" # OK
|
ARRAY["abc"]="abc" # OK
|
||||||
ARRAY["abx###"]="abc" # works in bash but will not saved to file
|
ARRAY["abx###"]="abc" # works in bash but will not saved to file
|
||||||
@ -1057,7 +1062,6 @@ cat file.jssh
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
*Hint*: Try `tr -dc "[:alnum:],.\r\n"` to strip invalid characters from key.
|
|
||||||
```bash
|
```bash
|
||||||
# strip key containing invalid characters
|
# strip key containing invalid characters
|
||||||
KEY="123abcABC,.#?(<>123ÄÖ*%&§"
|
KEY="123abcABC,.#?(<>123ÄÖ*%&§"
|
||||||
@ -1201,6 +1205,9 @@ Note: Existing content not in ARRAY is kept in file.
|
|||||||
|
|
||||||
*usage:* jssh_updateDB_async "ARRAY" "filename"
|
*usage:* jssh_updateDB_async "ARRAY" "filename"
|
||||||
|
|
||||||
|
*Note:* `jssh_updateDB` updates new or changed keys/value pairs only, it will not delete an existing key/value pair.
|
||||||
|
If you want to delete a existing key/value pair assign the unset value `${JSSJDB__UNSET}`.
|
||||||
|
|
||||||
*example:*
|
*example:*
|
||||||
```bash
|
```bash
|
||||||
# continued example from writeDB
|
# continued example from writeDB
|
||||||
@ -1211,18 +1218,30 @@ MYVALUES["newvalue"]="this is new"
|
|||||||
jssh_updateDB "MYVALUES" "${DATADIR:-.}/myvalues"
|
jssh_updateDB "MYVALUES" "${DATADIR:-.}/myvalues"
|
||||||
|
|
||||||
# show what's written
|
# show what's written
|
||||||
|
cat ${DATADIR:-.}/myvalues".jssh
|
||||||
["value1"] "value1"
|
["value1"] "value1"
|
||||||
["loveit"] "value2"
|
["loveit"] "value2"
|
||||||
["whynot"] "value3"
|
["whynot"] "value3"
|
||||||
["newvalue"] "this is new"
|
["newvalue"] "this is new"
|
||||||
|
|
||||||
# now writeDB
|
#######
|
||||||
cat "$DBfile"
|
# update does not delete key/value pairs
|
||||||
jssh_writeDB "MYVALUES" "${DATADIR:-.}/myvalues"
|
# uset in bash and update file
|
||||||
|
unset MYVALUES["newvalue"]
|
||||||
|
jssh_updateDB "MYVALUES" "${DATADIR:-.}/myvalues"
|
||||||
|
|
||||||
# show what's written, ups!
|
["value1"] "value1"
|
||||||
cat "$DBfile"
|
["loveit"] "value2"
|
||||||
["newvalue"] "this is new"
|
["whynot"] "value3"
|
||||||
|
["newvalue"] "this is new" # value exists!
|
||||||
|
|
||||||
|
# use JSSHDB_UNSET value
|
||||||
|
MYVALUES["newvalue"]="${JSSHDB_UNSET}"
|
||||||
|
jssh_updateDB "MYVALUES" "${DATADIR:-.}/myvalues"
|
||||||
|
|
||||||
|
["value1"] "value1"
|
||||||
|
["loveit"] "value2"
|
||||||
|
["whynot"] "value3"
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -1657,5 +1676,5 @@ The name of your bot is available as bash variable "$ME", there is no need to ca
|
|||||||
#### [Prev Best Practice](5_practice.md)
|
#### [Prev Best Practice](5_practice.md)
|
||||||
#### [Next Notes for Developers](7_develop.md)
|
#### [Next Notes for Developers](7_develop.md)
|
||||||
|
|
||||||
#### $$VERSION$$ v1.45-dev-36-gf7897fd
|
#### $$VERSION$$ v1.45-dev-41-g192fae8
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
# This file is public domain in the USA and all free countries.
|
# This file is public domain in the USA and all free countries.
|
||||||
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||||
#
|
#
|
||||||
#### $$VERSION$$ v1.45-dev-40-g7072442
|
#### $$VERSION$$ v1.45-dev-41-g192fae8
|
||||||
#
|
#
|
||||||
# source from commands.sh to use jsonDB functions
|
# source from commands.sh to use jsonDB functions
|
||||||
#
|
#
|
||||||
@ -369,7 +369,7 @@ Array2Json() {
|
|||||||
declare -n ARRAY="$1"
|
declare -n ARRAY="$1"
|
||||||
for key in "${!ARRAY[@]}"
|
for key in "${!ARRAY[@]}"
|
||||||
do
|
do
|
||||||
[[ ! "${key}" =~ ^${JSSH_KEYOK}+$ || "${ARRAY[${key}]}" = "${JSSHDB_UNSET}" ]] && continue
|
[[ ! "${key}" =~ ^${JSSH_KEYOK}+$ || "${ARRAY[${key}]}" == "${JSSHDB_UNSET}" ]] && continue
|
||||||
# in case value contains newline convert to \n
|
# in case value contains newline convert to \n
|
||||||
: "${ARRAY[${key}]//$'\n'/\\n}"
|
: "${ARRAY[${key}]//$'\n'/\\n}"
|
||||||
printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${_//\"/\\\"}"
|
printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${_//\"/\\\"}"
|
||||||
|
Loading…
Reference in New Issue
Block a user