|Welcome to the Monastery|
quick way to add a line to a file, if it's not already present (automating tweaking of /etc/ld.so.conf for installing Image::Magick)by tphyahoo (Vicar)
|on Nov 17, 2006 at 16:12 UTC||Need Help??|
tphyahoo has asked for the
wisdom of the Perl Monks concerning the following question:
Monks, I'm installing ImageMagick from source on a large number of machines. I have everything automated, except there's a small tweak to /etc/ld.so.conf that I am still doing by hand. I was just wondering if there was a way to automate that away as well.
This install from source and tweak /etc/ld.so.conf process is something I have to do for a number of dependencies, not just for imagemagick, so I will wind up with a number of scripts that look kind of like the one below.
So basically I need to add a line to a conf file, but I shouldn't add it if it's already there. I can think of many ways to do it, but wonder if there's one way that is particularly sweet and easy... you know... a one liner :)
I'm thinking perl, but if there's a way to do this with bash or sed or howsoever, fine with that as well.
As a bonus, here's my automated install of ImageMagick. If anybody has comments or improvements, fire away.
Oh, and before suggesting to me that I do "apt-get -y install perlmagick", that won't work for me in my situation, because it's a locally installed perl in a delicate state.
UPDATE: Thanks to mk for putting me on the right track :) Her solution was good, but it inserted lines rather than tacking them on at the end, which bugged me aesthetically.
I played around with her solution and some other ideas, and finally have something that I like. In the end, a combinatino of bash and perl seemed like the most natural solution to me. Hope this isn't too much of an obfu :) :
UPDATE 2: (deleted update 1) For those that still care, VSarkiss pointed me towards a less boneheaded way of doing this. My final result: