-1

I know this may sound weird but I am not understanding why this is occurring. So here is how it goes. I want to write a bash script to changes these variables on redis.conf which is located in /etc/redis/redis.conf. The script is like so:

sudo sed -e "s/^daemonize no$/daemonize yes/" \
  -e "s/^# unixsocket /var/run/redis-server.sock$/unixsocket /var/run/redis/redis.sock/" \
  -e "s/^# unixsocketperm 700$/unixsocketperm 770/" \
  -e "s/^# maxclients 10000$/maxclients 512/" \
  -e "s/^databases 16$/databases 128/" /etc/redis/redis.conf | sudo tee /etc/redis/redis.conf

Once ran, I receive an error on sed finding no substitution to change. I look in the /ect/redis/redis.conf file and everything is empty inside. No configuration whatsoever. Can someone help me understand what I am doing wrong. I would greatly appreciate it.

Roma
  • 535
  • 6
  • 18
  • 1
    Almost-duplicate: [How can I use a file in a command and redirect output to the same file without truncating it?](https://stackoverflow.com/q/6696842/3266847) – Benjamin W. Apr 14 '21 at 20:28
  • Your command has the form `sed -e "s/A/B/" redis.conf`; is there a file named `redis.conf` in the working directory? – Beta Apr 14 '21 at 20:30
  • 1
    You also have to either escape all your `/`, or use different delimiters for `s` – Benjamin W. Apr 14 '21 at 20:31
  • Thank you for the reply guys. @Beta yes, let me correct that I should have changed this but yes the file direction is like so: `/etc/redis/redis.conf`. I'll update that in my original post. (I still receive an error.) – Roma Apr 14 '21 at 20:36
  • @BenjaminW. I'll look into that. What would be this approach? – Roma Apr 14 '21 at 20:36
  • You can use any other character: `s|/a/b/c|/d/e/f|`, `s#foo#bar#`... – Benjamin W. Apr 14 '21 at 20:38
  • If you keep forward slashes, you have to escape like `s/\/a\/b\/c/\/d\/e\/f/` – Benjamin W. Apr 14 '21 at 20:39
  • @BenjaminW. That solved it Benjamin. Also, I did not look into `sed`'s documentation of the difference between `-e` and `-i`. I tested it and it did work with one line but going to test all of them. I'll report back once I get this done. – Roma Apr 14 '21 at 20:59

1 Answers1

0

The issue has been resolved. The code looks like this now which works perfectly to what I need.

if [ ! -d "/etc/redis/redis.conf" ]; then
  sed -i "s|^# unixsocket /var/run/redis/redis-server.sock$|unixsocket /var/run/redis/redis.sock|
          s|^# unixsocketperm 700$|unixsocketperm 770|
          s|^# maxclients 10000$|maxclients 512|
          s|^databases 16$|databases 128|" /etc/redis/redis.conf
fi

Benjamin W. was a great help for helping me understanding delimiters and for digging deeper into sed commands. I hope this can help others who are configuring redis.conf for their environment.

Roma
  • 535
  • 6
  • 18