Skip to content

Add key search #10

Description

@PeterS6g

This script is helpful, but I also need to be able to get a list of keys in a bash script. For example, if redis contained a couple of keys:

keyfoo bar
keybaz qux

I want to be able to say something like:

out=$(./redi.sh -k "key*")
echo $out
keyfoo keybaz

(Then I might get keyfoo to find bar).

I don't completely understand how the redi.sh handles arrays, but something like this seems to work for my needs:

98a99,103
> function redis_keys_var() {
>         typeset REDIS_VAR="$@"
>         printf %b "*2\r\n\$4\r\nKEYS\r\n\$${#REDIS_VAR}\r\n$REDIS_VAR\r\n"
> }
>
129c134
< while getopts g:s:r:P:H:p:d:ha opt; do
---
> while getopts g:s:r:P:H:p:d:hak: opt; do
142a148,150
>                 k)
>                         REDIS_KEYS=${OPTARG}
>                         ;;
158c166
<                       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>] [-k <key>] [-p <password>] [-d <database_number>] [-H <hostname>] [-P <port>]"
165,166c173,175
< if [[ -z $REDIS_GET ]] && [[ -z $REDIS_SET ]]; then
<       echo "You must either GET(-g) or SET(-s)" >&2
---
>
> if [[ -z $REDIS_GET ]] && [[ -z $REDIS_SET ]] && [[ -z $REDIS_KEYS ]]; then
>         echo "You must GET(-g) get KEYS(-k) or SET(-s)" >&2
198a208,215
> if [[ ! -z $REDIS_KEYS ]]; then
>         redis_keys_var "$REDIS_KEYS" >&$FD
>         redis_read $FD
>
>         exec {FD}>&-
>         exit 0
> fi
>

I'd appreciate it if someone could add this or something like it...

Also: I've done some more testing of this, and found some bugs (when a key is not present, or when redis has not yet started) that can cause redi.sh to hang. My fixes are as follows:

@@ -62,11 +62,15 @@ do
                         ;;
                 '$')
                         bytecount=$(printf %b "$socket_data" | cut -f2 -d$ | tr -d '\r')
-                        redis_read_bulk "$bytecount" "$FILE_DESC"
+                        if (( bytecount > 0)); then
+                                redis_read_bulk "$bytecount" "$FILE_DESC"
+                        fi
                         ;;
                 '*')
                         paramcount=$(printf %b "$socket_data" | cut -f2 -d* | tr -d '\r')
-                        redis_read "$FILE_DESC" "$paramcount"
+                        if (( paramcount > 0)); then
+                                redis_read "$FILE_DESC" "$paramcount"
+                        fi
                         ;;
         esac
 
@@ -177,6 +181,10 @@ if [[ -z $REDIS_GET ]] && [[ -z $REDIS_SET ]] && [[ -z $REDIS_KEYS ]]; then
 fi
 
 exec {FD}<> /dev/tcp/"$REDIS_HOST"/"$REDIS_PORT"
+fd_rc="$?"
+if (( fd_rc != 0)); then
+        exit $fd_rc
+fi
 
 redis_select_db "$REDIS_DB" >&$FD
 redis_read $FD 1>/dev/null 2>&1

with those changes applied on top of my changes above.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions