Last active
September 5, 2021 15:20
-
-
Save itamarhaber/c33ab7a067483050c47d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Why? Education & zero redis-cli dependecy | |
# (it is a shame, however, that this functionality isn't a part of redis-cli... PR on the way) | |
HOST=127.0.0.1 | |
PORT=6379 | |
if [ $# -lt "2" ] | |
then | |
echo "Call a Redis command with its last argument being file contents" | |
echo "Usage: $0 <Redis command> <keys and arguments for command> <payload>" | |
echo | |
echo "Example: calling '$0 SET foo bar.txt' will set the key 'foo' with the contents of the file 'bar.txt'" | |
exit 1 | |
fi | |
POS=0 | |
RESP= | |
for ARG; | |
do | |
POS=$(($POS + 1)) | |
if [ $POS != "$#" ] | |
then | |
RESP="$RESP\$${#ARG}\r\n$ARG\r\n" | |
else | |
if [ -a $ARG ] | |
then | |
SIZE=`cat $ARG | wc -c` | |
RESP="*$POS\r\n$RESP\$$SIZE\r\n" | |
else | |
echo "File not found: $ARG" | |
exit 1 | |
fi | |
fi | |
done | |
TEMP="/tmp/$BASHPID.tmp" | |
echo -n -e $RESP > $TEMP | |
cat $ARG >> $TEMP | |
echo -n -e "\r\n" >> $TEMP | |
nc $HOST $PORT < $TEMP | |
rm $TEMP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment