mirror of
https://github.com/octoleo/telegram-bot-bash.git
synced 2024-05-28 16:00:52 +00:00
Array2Json, module jsshDB.sh
This commit is contained in:
parent
bbbc8aece1
commit
9e020a9644
21
bashbot.sh
21
bashbot.sh
|
@ -11,7 +11,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$$ v0.90-dev2-8-g9b05ae0
|
#### $$VERSION$$ v0.90-dev2-9-gbbbc8ae
|
||||||
#
|
#
|
||||||
# Exit Codes:
|
# Exit Codes:
|
||||||
# - 0 sucess (hopefully)
|
# - 0 sucess (hopefully)
|
||||||
|
@ -314,14 +314,29 @@ JsonGetLine() {
|
||||||
JsonGetValue() {
|
JsonGetValue() {
|
||||||
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]\([0-9.,]*\).*/\1/p'
|
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]\([0-9.,]*\).*/\1/p'
|
||||||
}
|
}
|
||||||
|
# read JSON.sh style data and asssign to an ARRAY
|
||||||
|
# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling
|
||||||
|
Json2Array() {
|
||||||
|
# shellcheck source=./commands.sh
|
||||||
|
[ "$1" = "" ] || source <( printf "$1"'=( %s )' "$(sed -E -e 's/\t/=/g' -e 's/=(true|false)/="\1"/')" )
|
||||||
|
}
|
||||||
|
# output ARRAY as JSON.sh style data
|
||||||
|
# $1 ARRAY name, must be declared with "declare -A ARRAY" before calling
|
||||||
|
Array2Json() {
|
||||||
|
local ARRAY, key
|
||||||
|
declare -n ARRAY="$1"
|
||||||
|
for key in "${!ARRAY[@]}"
|
||||||
|
do
|
||||||
|
printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${ARRAY[${key}]//\"/\\\"}"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
################
|
################
|
||||||
# processing of updates starts here
|
# processing of updates starts here
|
||||||
process_updates() {
|
process_updates() {
|
||||||
local max num debug="$1"
|
local max num debug="$1"
|
||||||
max="$(sed <<< "${UPDATE}" '/\["result",[0-9]*\]/!d' | tail -1 | sed 's/\["result",//g;s/\].*//g')"
|
max="$(sed <<< "${UPDATE}" '/\["result",[0-9]*\]/!d' | tail -1 | sed 's/\["result",//g;s/\].*//g')"
|
||||||
# shellcheck source=./commands.sh
|
Json2Array 'UPD' <<<"${UPDATE}"
|
||||||
source <( printf 'UPD=( %s )' "$(sed <<<"${UPDATE}" -E -e 's/\t/=/g' -e 's/=(true|false)/="\1"/')" )
|
|
||||||
for ((num=0; num<=max; num++)); do
|
for ((num=0; num<=max; num++)); do
|
||||||
process_client "$num" "${debug}"
|
process_client "$num" "${debug}"
|
||||||
done
|
done
|
||||||
|
|
44
modules/jsonDB.sh
Normal file
44
modules/jsonDB.sh
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
#!/bin/bash
|
||||||
|
# file: modules/jsshDB.sh
|
||||||
|
# do not edit, this file will be overwritten on update
|
||||||
|
|
||||||
|
# This file is public domain in the USA and all free countries.
|
||||||
|
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||||
|
#
|
||||||
|
#### $$VERSION$$ v0.90-dev2-9-gbbbc8ae
|
||||||
|
#
|
||||||
|
# source from commands.sh to use jsonDB functions
|
||||||
|
#
|
||||||
|
# jsonDB rovides simple functions to read and store bash Arrays
|
||||||
|
# from to file in JSON.sh output format
|
||||||
|
|
||||||
|
# read content of a file in JSON.sh format into given ARRAY
|
||||||
|
# $1 ARRAY name, must be delared with "declare -A ARRAY" upfront
|
||||||
|
# $2 filename, must be relative to BASHBOT_ETC, and not contain '..'
|
||||||
|
jssh_readDB() {
|
||||||
|
local DB="${BASHBOT_ETC:-.}/$2.jssh"
|
||||||
|
[ "$2" = "" ] && return 1
|
||||||
|
[[ "$2" = *'..'* ]] && return 1
|
||||||
|
[ ! -f "${DB}" ] && return 1
|
||||||
|
Json2Array "$1" <"${DB}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# write ARRAY content to a file in JSON.sh format
|
||||||
|
# $1 ARRAY name, must be delared with "declare -A ARRAY" upfront
|
||||||
|
# $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..'
|
||||||
|
jssh_writeDB() {
|
||||||
|
local DB="${BASHBOT_ETC:-.}/$2.jssh"
|
||||||
|
[ "$2" = "" ] && return 1
|
||||||
|
[[ "$2" = *'..'* ]] && return 1
|
||||||
|
[ ! -f "${DB}" ] && return 1
|
||||||
|
Array2Json "$1" >"${DB}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# $1 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..'
|
||||||
|
jssh_newDB() {
|
||||||
|
local DB="${BASHBOT_ETC:-.}/$1.jssh"
|
||||||
|
[ "$1" = "" ] && return 1
|
||||||
|
[[ "$2" = *'..'* ]] && return 1
|
||||||
|
[ -f "${DB}" ] && return 1 # already exist, do not zero out
|
||||||
|
printf '\n' >"${DB}"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user