Skip to content
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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ It is an extension of [redi.sh](https://github.com/crypt1d/redi.sh) which allows
-h : Tells the script that we are working with hashes, 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).
-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.
-f <name> : Set the hash field specified by <name> with the input from stdin.
-s <name> : Set the variable/array specified by <name> with the input from stdin.
-f <name> : Set the hash field 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 <number> : Specify a custom database number from range 0-15\. Default is 0
-P <port> : Specify a custom port to connect to. Default is 6379.
-w <name> : Deletes specified key from database
-W : Deletes all keys (same as '-w *')
```

## Example:
Expand Down
36 changes: 31 additions & 5 deletions redis.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ REDIS_HOST="${REDIS_HOST:-127.0.0.1}"
REDIS_PORT="${REDIS_PORT:-6379}"
REDIS_DB="${REDIS_DB:-0}"
REDIS_ARRAY_RANGE="0,-1"

REDIS_WIPEOUT=""

function redis_read_str() {
typeset REDIS_STR="$@"
Expand Down Expand Up @@ -179,7 +179,16 @@ function redis_set_hash() {
printf %b "*4\r\n\$4\r\nHSET\r\n\$${#redis_key}\r\n$redis_key\r\n\$${#redis_field}\r\n$redis_field\r\n\$${#redis_value}\r\n$redis_value\r\n"
}

while getopts g:s:r:P:H:p:d:f:ha opt; do
function redis_wipeout_all() {
printf %b "*1\r\n\$7\r\nFLUSHDB\r\n"
}

function redis_wipeout() {
typeset redis_key="$1"
printf %b "*2\r\n\$3\r\DEL\r\n\$${#redis_key}\r\n$redis_key\r\n"
}

while getopts g:s:r:P:H:p:d:f:ha:w:W opt; do
case $opt in
p)
REDIS_PW=${OPTARG}
Expand Down Expand Up @@ -211,18 +220,24 @@ while getopts g:s:r:P:H:p:d:f:ha opt; do
d)
REDIS_DB=${OPTARG}
;;
w)
REDIS_WIPEOUT=${OPTARG}
;;
W)
REDIS_WIPEOUT="*"
;;
*)
echo
echo USAGE:
echo " $0 [-a|-h] [-r <range>] [-s <var>] [-g <var>] [-f <field>] [-p <password>] [-d <database_number>] [-H <hostname>] [-P <port>]"
echo " $0 [-a|-h] [-W] [-w <var>] [-r <range>] [-s <var>] [-g <var>] [-f <field>] [-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_GET ]] && [[ -z $REDIS_SET ]] && [[ -z $REDIS_WIPEOUT ]]; then
echo "You must either GET(-g) or SET(-s) or WIPEOUT(-W, -w)" >&2
exit 1
fi

Expand All @@ -241,6 +256,17 @@ if [[ ! -z $REDIS_PW ]]; then
redis_read $FD 1>/dev/null 2>&1
fi

if [[ ! -z $REDIS_WIPEOUT ]]; then
if [[ "*" == $REDIS_WIPEOUT ]] ; then
redis_wipeout_all >&$FD
else
redis_wipeout "$REDIS_WIPEOUT" >&$FD
fi

exec {FD}>&-
exit 0
fi

if [[ ! -z $REDIS_GET ]]; then
if [[ $REDIS_ARRAY -eq 1 ]] || [[ $REDIS_HASH -eq 1 ]] ; then
if [[ $REDIS_ARRAY -eq 1 ]] ; then
Expand Down