Solved

Adding a line if not present

Posted on 2003-12-02
5
197 Views
Last Modified: 2010-04-22
Dear experts.

Please tell me how to make a bash function to add a line if that line is not present in a file.

The file is /etc/ld.so.conf, it is like this
=========
/usr/lib
/usr/X11R6/lib
=========

Here is my script that does not work.
-------------------
function add_ldconfig()
{
    cat /etc/ld.so.conf | while read line; do
        if [ "$1" = "$line" ]; then
            return 0
        fi
    done
    echo $1 >> /etc/ld.so.conf
}
--------------------

if the function is called like this
add_ldconfig /opt/kde/lib

it should modify the file into
=========
/usr/lib
/usr/X11R6/lib
/opt/kde/lib
=========

But
add_ldconfig /usr/X11/lib

would not change anything.

So far, my script always add the entry.
0
Comment
Question by:Kocil
  • 3
  • 2
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9864559
grep "line_to_add" file_to_add_to
if [ $? -eq 1 ]
then
       echo "line_to_add" >> file_to_add_to
fi
0
 
LVL 5

Author Comment

by:Kocil
ID: 9865122
Thanks sunny, but your script failed when the file_to_add_to was
==========
/usr/local/lib
/usr/X11R6/lib
/usr/lib/qt/lib
==========

and the line_to_add = /usr/lib.

I found options -x works though.

by the way ... I wonder why in my script,
'return' does not quit immediatelly from the function ?
It only quits from the while block. Same as 'break'.

A modified like this below was also not working.

   present=0
   cat /etc/ld.so.conf | while read line; do
       if [ "$1" = "$line" ]; then
           present=1
           echo $present
           break
       fi
   done
   echo $present
   if [ present = 0 ]; then
       echo $1 >> /etc/ld.so.conf
   fi

The present inside the loop was changed to 1, but outside the loop, it backs to 0.
Strange.
My bash is version 2.05b.0


Thanks.

0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 200 total points
ID: 9873270
>The present inside the loop was changed to 1, but outside the loop, it backs to 0.
http:/Q_20681247.html

script that I posted in my last post works perfectly fine at my system ... bash 2.05.8
what was the error encountered at your end ?
check carefully for spaces
0
 
LVL 5

Author Comment

by:Kocil
ID: 9875829
> what was the error encountered at your end ?

grep /usr/lib file_to_add_to

would match any line with /usr/lib, including /usr/lib/qt/lib
So it' won't attach it.

However
grep -x /usr/lib file_to_add_to

works perfectly.

Thanks for the last link for the pipe problem. All clear now.
You are great.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9880140
oh ... never thought of that scenario...
when you mentioned -x, I though -x for debugging :-D and thus could not make much sense out of the last post...

alls well that ends well :o)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now