Solved

Adding a line if not present

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

Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

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 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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

726 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