Skip to content

Instantly share code, notes, and snippets.

@bikong0411
Last active August 12, 2016 09:03
Show Gist options
  • Save bikong0411/635180fbded9a779727b5b3182086005 to your computer and use it in GitHub Desktop.
Save bikong0411/635180fbded9a779727b5b3182086005 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ "$#" -lt 2 ]
then
echo "Scan keys in Redis matching a pattern using SCAN (safe version of KEYS)"
echo "Usage: $0 <host> [port] [database] [pattern]"
exit 1
fi
host=${1:-}
port=${2:-6379}
database=${3:-0}
pattern=${3:-\*}
cursor=-1
keys=""
while [[ "$cursor" -ne 0 ]]; do
if [[ "$cursor" -eq -1 ]]
then
cursor=0
fi
reply=$(redis-cli -h "$host" -p "$port" -n "$database" SCAN "$cursor" MATCH "$pattern")
cursor=$(expr "$reply" : '\([0-9]*[0-9 ]\)')
keys=${reply##[0-9]*[0-9 ]}
echo $keys
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment