Skip to content

Instantly share code, notes, and snippets.

@romuloceccon
Created August 2, 2020 07:27
Show Gist options
  • Save romuloceccon/84e8a964708fa99fb46bf90bf75c5dc2 to your computer and use it in GitHub Desktop.
Save romuloceccon/84e8a964708fa99fb46bf90bf75c5dc2 to your computer and use it in GitHub Desktop.
How to connect both stdin and stdout of some process to netcat in Bash 3, or, how to emulate netcat traditional's -e switch
#! /bin/bash
if (( $# ))
then
echo "usage: mkfifo r.fifo ; nc -q 1 localhost 6379 < r.fifo | $0 > r.fifo" >&2
exit 2
fi
KEY="test-key"
VAL="val"
IFS=$'\r'
while :
do
echo "GET $KEY"
read -r LINE
if [[ "$LINE" =~ ^\$[[:digit:]]+$ ]]
then
read -r LINE
echo "$KEY exists: $LINE" >&2
break
elif [[ "$LINE" =~ ^\$- ]]
then
echo "$KEY does not exist" >&2
echo "SET $KEY $VAL"
read -r LINE
if [[ "$LINE" != '+OK' ]]
then
echo "SET error: $LINE" >&2
exit 1
fi
else
echo "GET error: $LINE" | hd >&2
exit 1
fi
done
echo "DEL $KEY"
read -r LINE
echo "$KEY deleted: $LINE" >&2
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment