forked from caquino/redis-bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis-bash-lib
executable file
·52 lines (51 loc) · 1.53 KB
/
redis-bash-lib
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
#!/bin/bash
# https://github.com/caquino/redis-bash
# -- redis-client
#
# returns an array
#
# Args:
# value -- file descriptor
# value -- message to be sent (only read if blank)
#
function redis-client() {
if [ ${#FUNCNAME[@]} -lt 100 ]; then
FD=${1}
shift;
if [ ${#} -ne 0 ]; then # always use unified protocol and let the server validate the number of parameters
local ARRAY=( "${@}" )
local CMD=("*$[${#ARRAY[@]}]")
local i=0
for ((i=0;i<${#ARRAY[@]};i++)); do
CMD=( "${CMD[@]}" "\$${#ARRAY[${i}]}" "${ARRAY[${i}]}" )
done
printf "%s\r\n" "${CMD[@]}" >&${FD}
fi
local ARGV
read -r -u ${FD}
REPLY=${REPLY:0:${#REPLY}-1}
case ${REPLY} in
-*|\$-*) # error message
echo "${REPLY:1}"
return 1;;
\$*) # message size
[ ${BASH_VERSINFO} -eq 3 ] && SIZEDELIM="n"
[ ${REPLY:1} -gt 0 ] && read -r -${SIZEDELIM:-N} $[${REPLY:1}+2] -u ${FD} # read again to get the value itself
ARGV=( "${REPLY:0:$[${#REPLY}-$[${BASH_VERSINFO}-2]]}" );;
:*) # integer message
ARGV=( "${REPLY:1}" );;
\**) # bulk reply - recursive based on number of messages
unset ARGV
for ((ARGC="${REPLY:1}";${ARGC}>0;ARGC--)); do
ARGV=("${ARGV[@]}" $(redis-client ${FD}))
done;;
+*) # standard message
ARGV=( "${REPLY:1}" );;
*) # wtf? just in case...
ARGV=( "${ARGV[@]}" "${REPLY}" );;
esac
printf "%s\n" "${ARGV[@]}"
else
printf "ERROR: Recursive function call limit.\n"
fi
}