Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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 ( #584749=perlquestion: print w/ replies, xml ) 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.

cat install-imagemagick.sh #!/bin/bash su -c '( # uncomment the following line if you need to get the file #wget 'http://downloads.sourceforge.net/imagemagick/ImageMagick-6.3 +.0-5.tar.gz' tar -xzvf ImageMagick-6.3.0-5.tar.gz cd ImageMagick-6.3.0 ./configure make make install cd - rm -rf ImageMagick-6.3.0 )' 2>&1 | tee installImageMagickOut.txt echo " ***************************************************************** image magic should be installed from source. now let's test if perl -MImageMagick works you may get errors about a missing .so file if this happens, try doing ( updatedb; locate the-so-file.so; ) then change /etc/ld.so.conf to include the directory containing that f +ile then do ldconfig and try again ***************************************************************** " /usr/local/path/to/custom/perl -MImage::Magick -e ''
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:

$ cat so-config-imagemagick.sh #!/bin/bash ./so-config-from-subdir.sh if [ ! -f /etc/ld.so.conf.d/imagemagick.conf ]; then echo /usr/local/lib >> /etc/ld.so.conf.d/imagemagick.conf ldconfig fi result=`/usr/angebote/perlroot/bin/perl -MImage::Magick -e ''` if [ -n "$result" ]; then echo result: $result exit echo "seems there was a problem configuring shared libraries for imag +e magick running /usr/angebote/perlroot/bin/perl -MImage::Magick -e '' r +esulted in error $result if you got an error about a missing .so file, try doing ( updat +edb; locate the-so-file.so; ) then change /etc/ld.so.conf to include the directory containing + that file then do ldconfig and try again " fi $ cat so-config-from-subdir.sh #!/bin/bash # if we aren't reading from the ld.so.conf.d subdir, start doing so # if the result of the grep is a zero length string config_from_subdir=`grep 'include /etc/ld.so.conf.d/\*.conf' /etc/ld.s +o.conf`; if [ -z "$config_from_subdir" ]; then echo 'include /etc/ld.so.conf.d/*.conf' >> /etc/ld.so.conf fi mkdir -p /etc/ld.so.conf.d $

Comment on 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)
Select or Download Code
Re: 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 mk. (Friar) on Nov 17, 2006 at 16:48 UTC
    one liner, as requested (:
    (it should add one line to the file in case it's not already there and that's all, right?!)
    perl -pi -e '$line = 1 if /pattern/; print "the line\n" if !$line&&eof' /etc/ld.so.conf

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    *women.pm
Re: 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 VSarkiss (Monsignor) on Nov 17, 2006 at 18:28 UTC
      actually... no, I hadn't thought of this. yes, it's obvious. after it's been explained. thanks :)
Re: automating tweaking of /etc/ld.so.conf for installing Image::Magick
by rhesa (Vicar) on Nov 17, 2006 at 18:37 UTC
    on my system (CentOS), I do this:
    echo "/usr/local/lib" > /etc/ld.so.conf.d/ImageMagick.conf ldconfig
    As that goes in its own file, there's no need for trickery.

    Oh, and of course my /etc/ld.so.conf is just this:

    include ld.so.conf.d/*.conf

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://584749]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (17)
As of 2014-07-10 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (214 votes), past polls