?
Solved

Adding a line if not present

Posted on 2003-12-02
5
Medium Priority
?
209 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 800 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

850 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