Solved

Adding a line if not present

Posted on 2003-12-02
5
199 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

861 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

22 Experts available now in Live!

Get 1:1 Help Now