?
Solved

Adding a line if not present

Posted on 2003-12-02
5
Medium Priority
?
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

800 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