-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathshdb.sh
executable file
·561 lines (489 loc) · 12.8 KB
/
shdb.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#!/bin/bash
set -e
#=============================================================
# SHDB
#
# A simple, lightweight key-value DB for shell programming.
#
# Install:
#
# make SIZE=10 (MB)
#
# Usage:
#
# shdb status
# shdb [-s|--shell] isset [key]
# shdb [-s|--shell] set [key] [value]
# shdb [-s|--shell] get [key]
# shdb [-s|--shell] delete [key]
# shdb [-s|--shell] count
# shdb uninstall
#
# Author: YHSPY
# License: MIT
#=============================================================
# set global variables.
OS_TYPE=$(uname)
SUDO_USER=$(echo -n $SUDO_USER)
if [ -n "$SUDO_USER" ] ; then
if [ "${OS_TYPE}" = "Linux" ] ; then
HOME_DIR=$(getent passwd $SUDO_USER | cut -d: -f6)
else
HOME_DIR="/Users/$SUDO_USER"
fi
else
HOME_DIR=$(echo -n ~)
fi
BASE64_ENCODED=''
BASE64_DECODED=''
CONFIG_KEY=''
CONFIG_VAL=''
typeset -i DB_CURRENT_SIZE=0
typeset -i BOOL_TRUE=0
typeset -i BOOL_FALSE=1
typeset -i EXIT_NORMAL=0
typeset -i EXIT_ERROR=1
# set default setting pairs;
NAME="shdb"
INSTALL_DIR="/usr/local/bin/"
VERSION="1.4"
RELEASE="2020/10/01"
typeset -i AVSIZE=1000 # 1KB by default.
# set other configurations.
DB_CONF_FILE_NAME="${HOME_DIR}/.shdb.master.conf"
DB_DATA_FILE_NAME="${HOME_DIR}/.shdb.master.db"
DB_MAIN_ENTRY="${INSTALL_DIR}${NAME}"
# function definitions.
_func_has_installed() {
if [ -f $DB_CONF_FILE_NAME ] && [ -f $DB_DATA_FILE_NAME ] ; then
return $BOOL_TRUE
else
return $BOOL_FALSE
fi
}
_func_retrieve_db_system_item() {
if _func_has_installed ; then
# find value in db configuration file.
local _GREP_INFO=$(grep -w -n "$1" $DB_CONF_FILE_NAME)
if [ -n "$_GREP_INFO" ] ; then
CONFIG_VAL=${_GREP_INFO##*=}
fi
else
_func_report_error NOT_INSTALLED
fi
}
_func_base64_encode() {
BASE64_ENCODED=$(echo -n "$1" | base64)
}
_func_base64_decode() {
BASE64_DECODED=$(echo -n "$1" | base64 --decode)
}
_func_calc_db_size() {
if _func_has_installed ; then
local _DU_INFO
if [ "${OS_TYPE}" = "Linux" ] ; then
_DU_INFO=$(du --apparent-size --block-size=1 $DB_DATA_FILE_NAME)
else
_DU_INFO=$(du -k $DB_DATA_FILE_NAME)
fi
DB_CURRENT_SIZE=${_DU_INFO%% *}
else
_func_report_error NOT_INSTALLED
fi
}
install() {
if _func_has_installed ; then
_func_report_error ALREADY_INSTALLED
else
# checklist.
if [ $# -eq 2 ] ; then
if [ "${1}" = "--size" ] || [ "${1}" = "-s" ] ; then
local AVSIZE_VAL=${2}
if [ $AVSIZE_VAL -eq 0 ] || [ $AVSIZE_VAL -ge 1025 ] ; then
_func_report_error PARAMS_ERR
exit $EXIT_ERROR
else
AVSIZE=$(($AVSIZE_VAL * 1000))
fi
fi
fi
printf "[SHDB DATEBASE FILE]\n" > $DB_DATA_FILE_NAME
cat > $DB_CONF_FILE_NAME << EOF
[SHDB CONFIGURATION FILE]
NAME=shdb
VARSION=${VERSION}
RELEASE=${RELEASE}
AUTHOR=YHSPY
AVSIZE=${AVSIZE}
DATE=$(date)
EOF
# move to /usr/local/bin, and grant exection privilege.
cp -f ${0} $DB_MAIN_ENTRY
chmod +x $DB_MAIN_ENTRY
_func_report_info INSTALLED
fi
}
update() {
if _func_has_installed ; then
if [ "${0}" != "$DB_MAIN_ENTRY" ] ; then
# update source file.
cp -f ${0} $DB_MAIN_ENTRY
chmod +x $DB_MAIN_ENTRY
_func_report_info UPDATED
fi
else
_func_report_info NOT_INSTALLED
fi
}
uninstall() {
rm -f $DB_CONF_FILE_NAME
rm -f $DB_DATA_FILE_NAME
rm -f $DB_MAIN_ENTRY
_func_report_info UNINSTALLED
}
isset() {
if _func_has_installed ; then
_func_base64_encode "$1"
# find k-v in db.
local GREP_INFO=$(grep -o "|${BASE64_ENCODED}[^|]*|" $DB_DATA_FILE_NAME)
if [ -n "$GREP_INFO" ] ; then
if [ "$2" = --shell ] ; then
exit $EXIT_NORMAL
else
printf "%s\n" [True]
fi
else
if [ "$2" = --shell ] ; then
exit $EXIT_ERROR
else
printf "%s\n" [False]
fi
fi
else
_func_report_error NOT_INSTALLED
fi
}
set() {
_func_retrieve_db_system_item AVSIZE
_func_calc_db_size
if [ $DB_CURRENT_SIZE -ge $CONFIG_VAL ] ; then
_func_report_error DB_OVERFLOW
exit $EXIT_ERROR
fi
if _func_has_installed ; then
_func_base64_encode $1
local _SHDB_KEY=$BASE64_ENCODED
_func_base64_encode $2
local _SHDB_VAL=$BASE64_ENCODED
# find existing item in db.
local _GREP_INFO=$(grep -o "|${_SHDB_KEY}[^|]*|" $DB_DATA_FILE_NAME)
if [ -n "$_GREP_INFO" ] ; then
if [ "${OS_TYPE}" = "Linux" ] ; then
sed -i -e "s#${_GREP_INFO}#|${_SHDB_KEY}:${_SHDB_VAL}|#" ${DB_DATA_FILE_NAME}
else
sed -i "" -e "s#${_GREP_INFO}#|${_SHDB_KEY}:${_SHDB_VAL}|#" ${DB_DATA_FILE_NAME}
fi
else
echo "|${_SHDB_KEY}:${_SHDB_VAL}|" >> $DB_DATA_FILE_NAME
fi
if [ "$3" = --shell ] ; then
exit $EXIT_NORMAL
else
printf "%s\n" "[OK]"
fi
else
_func_report_error NOT_INSTALLED
fi
}
get() {
if _func_has_installed ; then
_func_base64_encode $1
local _SHDB_KEY=$BASE64_ENCODED
# find existing item in db.
local _GREP_INFO=$(grep -o "|${_SHDB_KEY}[^|]*|" $DB_DATA_FILE_NAME)
if [ -n "$_GREP_INFO" ] ; then
local _SHDB_VAL_TEMP=${_GREP_INFO##*:}
local _SHDB_VAL=${_SHDB_VAL_TEMP%%|*}
_func_base64_decode $_SHDB_VAL
if [ "$2" = --shell ] ; then
printf "%s" "$BASE64_DECODED"
else
printf "%s\n" "$BASE64_DECODED"
fi
else
if [ "$2" = --shell ] ; then
exit $EXIT_ERROR
else
printf "[Empty]\n"
fi
fi
else
_func_report_error NOT_INSTALLED
fi
}
delete() {
if _func_has_installed ; then
_func_base64_encode $1
local _SHDB_KEY=$BASE64_ENCODED
# find existing item in db.
local _GREP_INFO=$(grep -o "|${_SHDB_KEY}[^|]*|" $DB_DATA_FILE_NAME)
if [ -n "$_GREP_INFO" ] ; then
if [ "${OS_TYPE}" = "Linux" ] ; then
sed -i -e "/${_GREP_INFO}/d" $DB_DATA_FILE_NAME
else
sed -i "" -e "/${_GREP_INFO}/d" $DB_DATA_FILE_NAME
fi
if [ "$2" = --shell ] ; then
exit $EXIT_NORMAL
else
printf "[Deleted]\n"
fi
else
if [ "$2" = --shell ] ; then
exit $EXIT_ERROR
else
printf "[Empty]\n"
fi
fi
else
_func_report_error NOT_INSTALLED
fi
}
count() {
if _func_has_installed ; then
local _COUNT_TEMP=$(grep -o "|" $DB_DATA_FILE_NAME | grep -c "|")
local _COUNT_ITEM=$(($_COUNT_TEMP / 2))
if [ "$1" = --shell ] ; then
printf "$_COUNT_ITEM"
else
printf "[Count] $_COUNT_ITEM\n"
fi
fi
}
test() {
local _KEY="SHDB"
printf "\n"
echo "[isset] Let's find if the KV \"count\" has been stored in SHDB ..."
if shdb -s isset count ; then
echo "[result] Already exist... succeed"
else
echo "[result] (succeed)"
fi
printf "\n"
sleep 1
echo "[set] Let's reset/set the KV \"count\" with a value in SHDB ..."
# Set a key in SHDB
if shdb -s set count "${_KEY}" ; then
echo "[result] (succeed)"
else
echo "[result] (failed)"
fi
printf "\n"
sleep 1
echo "[get] Let's get the value of KV \"count\" from SHDB ..."
if shdb -s isset count ; then
local _VAL=$(shdb -s get count)
echo "[result] ${_VAL} (succeed)"
else
echo "[result] (failed)"
fi
printf "\n"
sleep 1
echo "[delete] Let's delete the KV \"count\" in SHDB ..."
if shdb -s isset count ; then
if shdb -s delete count ; then
echo "[result] (succeed)"
else
echo "[result] (failed)"
fi
fi
printf "\n"
}
_func_print_status() {
if _func_has_installed ; then
_func_retrieve_db_system_item AVSIZE
local _FILE_SIZE=$(du -h $DB_DATA_FILE_NAME)
local _DB_MAXIMUM_SIZE=$CONFIG_VAL
_DB_MAXIMUM_SIZE=$(($_DB_MAXIMUM_SIZE))
cat << EOF
[SHDB]
-----------------
Release Version: ${VERSION}
Release Date: ${RELEASE}
DB Current Size: ${_FILE_SIZE}
DB Maximum Size: ${_DB_MAXIMUM_SIZE}K
EOF
else
_func_report_error NOT_INSTALLED
fi
}
_func_report_error() {
case "$1" in
PARAMS_ERR )
cat << EOF
[shdb ERR] Invalid command or arguments... error
EOF
;;
ALREADY_INSTALLED )
cat << EOF
[shdb ERR] SHDB has already been installed... error
EOF
;;
NOT_INSTALLED )
cat << EOF
[shdb ERR] Core files missing, please re-install SHDB... error
EOF
;;
DB_OVERFLOW )
cat << EOF
[shdb ERR] SHDB has exceeded the maximum storage size... error
EOF
;;
esac
}
_func_report_info() {
case "$1" in
UNINSTALLED )
cat << EOF
[shdb INFO] SHDB uninstall... succeed
EOF
;;
INSTALLED )
cat << EOF
[shdb INFO] SHDB install... succeed
EOF
;;
UPDATED )
cat << EOF
[shdb INFO] SHDB update... succeed
EOF
;;
esac
}
console() {
while :
do
printf "%s" "shdb > "
read ORDER
if [ -n "$(echo -n "${ORDER}" | grep [[:space:]]*set[[:space:]][^[:space:]]*[[:space:]][^[:space:]]*)" ] ; then
local _SHDB_CMD=${ORDER#*set }
local _SHDB_KEY=${_SHDB_CMD%% *}
local _SHDB_VALUE=${_SHDB_CMD#${SHDB_KEY} }
set "$_SHDB_KEY" "$_SHDB_VALUE"
elif [ -n "$(echo -n "${ORDER}" | grep "[[:space:]]*get[[:space:]][^[:space:]]*")" ] ; then
local _SHDB_KEY=${ORDER#*get }
get "$_SHDB_KEY"
elif [ -n "$(echo -n "${ORDER}" | grep "[[:space:]]*delete[[:space:]][^[:space:]]*")" ] ; then
local _SHDB_KEY=${ORDER#*delete }
delete "$_SHDB_KEY"
elif [ -n "$(echo -n "${ORDER}" | grep "[[:space:]]*isset[[:space:]][^[:space:]]*")" ] ; then
local _SHDB_KEY=${ORDER#*isset }
isset "$_SHDB_KEY"
elif [ "$ORDER" = "count" ] ; then
count
elif [ "$ORDER" = "exit" ] ; then
break
else
_func_report_error PARAMS_ERR
fi
done
}
# save "Interal Field Separator".
PRE_IFS=$IFS
IFS=" "
# paly with parameters.
if [ "$1" = "-s" ] || [ "$1" = "--shell" ] ; then
if [ $# -eq 4 ] ; then
case "$2" in
set )
set "${3}" "${4}" --shell
;;
* )
_func_report_error PARAMS_ERR
;;
esac
elif [ $# -eq 3 ] ; then
case "$2" in
get )
get "${3}" --shell
;;
delete )
delete "${3}" --shell
;;
isset )
isset "${3}" --shell
;;
* )
_func_report_error PARAMS_ERR
;;
esac
elif [ $# -eq 2 ] ; then
case "$2" in
count )
count --shell
;;
esac
else
_func_report_error PARAMS_ERR
fi
else
if [ $# -eq 3 ] ; then
case "$1" in
set )
set "${2}" "${3}"
;;
install )
install "${2}" "${3}"
;;
* )
_func_report_error PARAMS_ERR
;;
esac
elif [ $# -eq 2 ] ; then
case "$1" in
get )
get "${2}"
;;
delete )
delete "${2}"
;;
isset )
isset "${2}"
;;
* )
_func_report_error PARAMS_ERR
;;
esac
elif [ $# -eq 1 ] ; then
case "$1" in
install )
install
;;
update )
update
;;
status )
_func_print_status
;;
count )
count
;;
uninstall )
uninstall
;;
console )
console
;;
test )
test
;;
* )
_func_report_error PARAMS_ERR
;;
esac
else
_func_print_status
fi
fi
IFS=$PRE_IFS
exit $EXIT_NORMAL