forked from caquino/redis-bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis-pubsub-test
executable file
·52 lines (52 loc) · 1.42 KB
/
redis-pubsub-test
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
# run this file passing the channel to subscribe as argument
# ./redis-pubsub-test testchannel
# and on another shell publish a message on the same channel
# ./redis-bash-cli -h localhost PUBLISH testchannel Hello World
source /usr/share/redis-bash/redis-bash-lib 2> /dev/null
if [ $? -ne 0 ]; then
LIBFOLDER=${0%/${0##*/}}
source ${LIBFOLDER}/redis-bash-lib 2> /dev/null
if [ $? -ne 0 ]; then
echo "can't find redis-bash-lib in /usr/share/redis-bash or ${LIBFOLDER}"
exit 127
fi
fi
REDISHOST=localhost
REDISPORT=6379
while getopts ":h:p:" opt
do
case ${opt} in
h) REDISHOST=${OPTARG};;
p) REDISPORT=${OPTARG};;
esac
done
shift $((${OPTIND} - 1))
while true
do
exec 5>&-
if [ "${REDISHOST}" != "" ] && [ "${REDISPORT}" != "" ]
then
exec 5<>/dev/tcp/${REDISHOST}/${REDISPORT} # open fd
else
echo "Wrong arguments"
exit 255
fi
redis-client 5 SUBSCRIBE ${1} > /dev/null # subscribe to the pubsub channel in fd 5
while true
do
unset ARGV
OFS=${IFS};IFS=$'\n' # split the return correctly
ARGV=($(redis-client 5))
IFS=${OFS}
if [ "${ARGV[0]}" = "message" ] && [ "${ARGV[1]}" = "${1}" ]
then
echo "Message from pubsub channel: ${ARGV[2]}"
elif [[ -z ${ARGV} ]]
then
sleep 1
break
fi
done
done