I encountered with behavior incomprehensible to me.
NODES=6
PORT=6379
until [ $NODES -eq 0 ]
do
((NEWPORT=PORT++))
cp cluster-config.conf redis-$NEWPORT.conf
sed -i -e "s/$PORT/$NEWPORT/g" redis-$NEWPORT.conf
((NODES--))
done
This doesn’t work
But this works
NODES=6
PORT=6379
until [ $NODES -eq 0 ]
do
((NEWPORT=PORT++))
cp cluster-config.conf redis-$NEWPORT.conf
sed -i -e "s/6379/$NEWPORT/g" redis-$NEWPORT.conf
((NODES--))
done
What’s the difference between
sed -i -e "s/$PORT/$NEWPORT/g" redis-$NEWPORT.conf
vs
sed -i -e "s/6379/$NEWPORT/g" redis-$NEWPORT.conf
2
Answers
You do a
PORT++
, which makesPORT
to be increased by one.By the time you use
sed
, it’s expanded for 6380, and then it’s not found in the file.You want to create 6 copies of a configuration file, with the existing port 6379 replaced with one of 6380-6385. That’s much more simply written as