skip to Main Content

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


  1. You do a PORT++, which makes PORT 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.

    Login or Signup to reply.
  2. 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

    port=6379
    nodes=6
    for ((newport=port+1; newport <= port + nodes; newport++)); do
      sed -e "s/$port/$newport/g" cluster-config.conf > redis-$newport.conf
    done
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search