Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redi.sh: add LLEN (-l) and disable select with -D #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ Redi.sh is a primitive Redis client, written entirely in Bash. It allows you to
> By default redi.sh reads input from stdin and interprets it as a variable or array (if -a is used). To avoid setting redis hostname and port number with each command, you can export REDIS_HOST and REDIS_PORT variables.

```
./redi.sh [-a] [-g <variable|array>] [-p <password>] [-H <hostname>] [-P <port>]
./redi.sh [-a] [-l <array>] [-g <variable|array>] [-p <password>] [-H <hostname>] [-P <port>] [-D]

-a : Tells the script that we are working with arrays, instead of regular variables.
-r <min,max> : When used with -a, defines the range of elements to get from the array. Default is all (0,-1).
-l <name> : Issue LLEN on <name> array and output it to stdout.
-g <name> : Get the variable/array specified by <name> and output it to stdout.
-s <name> : Set the variable/array specified by <name> with the input from stdin.
-p <password> : Use "AUTH <password>" before running the SET/GET command to authenticate to redis.
-H <hostname> : Specify a custom hostname to connect to. Default is localhost.
-d <number> : Specify a custom database number from range 0-15\. Default is 0
-D : Disable selecting database
-P <port> : Specify a custom port to connect to. Default is 6379.
```

Expand Down
36 changes: 29 additions & 7 deletions redi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
REDIS_HOST="${REDIS_HOST:-127.0.0.1}"
REDIS_PORT="${REDIS_PORT:-6379}"
REDIS_DB="${REDIS_DB:-0}"
REDIS_SELECT=1
CLIENT_VERSION=0.4
REDIS_ARRAY_RANGE="0,-1"

Expand Down Expand Up @@ -101,6 +102,11 @@ function redis_get_var() {
printf %b "*2\r\n\$3\r\nGET\r\n\$${#REDIS_VAR}\r\n$REDIS_VAR\r\n"
}

function redis_llen() {
typeset REDIS_VAR="$@"
printf %b "*2\r\n\$4\r\nLLEN\r\n\$${#REDIS_VAR}\r\n$REDIS_VAR\r\n"
}

function redis_set_var() {
typeset REDIS_VAR="$1"
shift
Expand All @@ -126,7 +132,7 @@ function redis_set_array() {
done
}

while getopts g:s:r:P:H:p:d:ha opt; do
while getopts g:l:s:r:P:H:p:d:Dha opt; do
case $opt in
p)
REDIS_PW=${OPTARG}
Expand All @@ -140,6 +146,9 @@ while getopts g:s:r:P:H:p:d:ha opt; do
g)
REDIS_GET=${OPTARG}
;;
l)
REDIS_LLEN=${OPTARG}
;;
a)
REDIS_ARRAY=1
;;
Expand All @@ -149,34 +158,47 @@ while getopts g:s:r:P:H:p:d:ha opt; do
s)
REDIS_SET=${OPTARG}
;;
d)
d)
REDIS_DB=${OPTARG}
;;
D)
REDIS_SELECT=0
;;
h)
echo
echo USAGE:
echo " $0 [-a] [-r <range>] [-s <var>] [-g <var>] [-p <password>] [-d <database_number>] [-H <hostname>] [-P <port>]"
echo " $0 [-a] [-r <range>] [-s <var>] [-g <var>] [-l <var>] [-p <password>] [-d <database_number>] [-H <hostname>] [-P <port>]"
echo
exit 1
;;
esac
done

if [[ -z $REDIS_GET ]] && [[ -z $REDIS_SET ]]; then
echo "You must either GET(-g) or SET(-s)" >&2
if [[ -z $REDIS_LLEN ]] && [[ -z $REDIS_GET ]] && [[ -z $REDIS_SET ]]; then
echo "You must either LLEN (-l), GET(-g) or SET(-s)" >&2
exit 1
fi

exec {FD}<> /dev/tcp/"$REDIS_HOST"/"$REDIS_PORT"

redis_select_db "$REDIS_DB" >&$FD
redis_read $FD 1>/dev/null 2>&1
# Disable selecting database
if [[ $REDIS_SELECT -eq 1 ]]; then
redis_select_db "$REDIS_DB" >&$FD
redis_read $FD 1>/dev/null 2>&1
fi

if [[ ! -z $REDIS_PW ]]; then
redis_compose_cmd "$REDIS_PW" >&$FD
redis_read $FD 1>/dev/null 2>&1
fi

if [[ ! -z $REDIS_LLEN ]]; then
redis_llen "$REDIS_LLEN" >&$FD
redis_read $FD
exec {FD}>&-
exit 0
fi

if [[ ! -z $REDIS_GET ]]; then
if [[ $REDIS_ARRAY -eq 1 ]]; then
redis_get_array "$REDIS_GET" "$REDIS_ARRAY_RANGE" >&$FD
Expand Down