2

What's the best approach to update an /etc/rc.conf configuration file programmatically?

Specifically, on an arch linux machine, I want to be able to programmatically update

DAEMONS=(syslog-ng network sshd ntpd netfs crond)

to

DAEMONS=(syslog-ng network sshd ntpd netfs crond postgresql)

after postgresql is successfully installed via pacman.

I presume I can write a function that does something like:

line="DAEMONS=(syslog-ng network sshd ntpd netfs crond)"

sed -i "/${line}/ s/)/ postgresql)/" /etc/rc.conf

specifically to handle this postgresql scenario.

However, going one step further, is there a more generic way (using a library if there's one you can recommend) that programmatically includes my service (such as memcached, or like a task server like zeromq etc) in the DAEMONS parameter in my /etc/rc.conf file?

Calvin Cheng
  • 35,640
  • 39
  • 116
  • 167

1 Answers1

1

I wouldn't know about a generic way (there seems to be very few tools which do any parsing and modification of shell code), but one way to update a simple array like this one could be to actually read it, change it, then write back the whole line - Something like this:

source /etc/rc.conf
DAEMONS+=(postgresql)
sed -i -e s/'^DAEMONS=.*'/"DAEMONS=(${DAEMONS[@]})"/ /etc/rc.conf
l0b0
  • 55,365
  • 30
  • 138
  • 223
  • Someone on #archlinux irc channel recommends `systemd` - https://wiki.archlinux.org/index.php/Systemd Am reading up and experimenting on it at the moment. – Calvin Cheng Apr 24 '12 at 08:00
  • `systemd` usage on a linode server instance requires that I use a custom kernel with `pv-grub` so a bash script is a simpler approach if I don't want to load up a custom kernel with `pv-grub`. – Calvin Cheng Apr 25 '12 at 01:26