LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to use different versions of grep on same system? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-use-different-versions-of-grep-on-same-system-758821/)

Andrew Dufresne 09-30-2009 10:35 PM

How to use different versions of grep on same system?
 
Hi,

I have got two versions of grep on my system.

grep 2.5.1 which is in /bin
grep 2.5.4 which is in /usr/local/bin . I complied it from source code.

Question 1
Now, 'yum info grep' still shows the older 2.5.1a version. How can I tell yum that a new version of grep in on board now?

Question 2
How can I remove the old grep? I tried using 'yum remove grep' but then it showed a huge list that too will be removed with grep so I didn't pursue this way. What will be the proper way?

Question 3
When I run grep as a root, it uses 2.5.4. Although /bin and /usr/local/bin both are in root's $PATH.
But when I run grep as user, it uses 2.5.1. Here too /bin and /usr/local/bin both are in its $PATH.

What is the reason of this different behaviour?

How can I ensure that every user gets grep 2.5.4 without using its absolute path?

Regards

kaz2100 09-30-2009 11:20 PM

Hya!

Answer 1. use absolute path.

Answer 2. may not be easy.

Answer 3. The one found first will kick in. Check PATH variable and which one comes first. (/bin and /usr/local/bin)

Happy Penguins!

Andrew Dufresne 09-30-2009 11:32 PM

Thanks for the reply.

Quote:

Originally Posted by kaz2100 (Post 3702777)
Answer 3. The one found first will kick in. Check PATH variable and which one comes first. (/bin and /usr/local/bin)

Well in both cases; root and user, /usr/local/bin comes first. But still user uses /bin version unlike root

Here is the $PATH of root

Quote:

$ echo $PATH
/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/ccache:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
and here is the $PATH of user

Quote:

$ echo $PATH
/usr/lib/qt-3.3/bin:/usr/kerberos/bin:/usr/lib/ccache:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/Andy/bin

Quote:

Originally Posted by kaz2100 (Post 3702777)
Quote:

Originally Posted by Andrew Dufresne (Post 3702756)
Hi,
Question 1
Now, 'yum info grep' still shows the older 2.5.1a version. How can I tell yum that a new version of grep in on board now?

Answer 1. use absolute path.

I tried
Code:

yum info /usr/local/bin/grep
But it gave me, "Error : No matching Packages to list".

So what do you suggest now?

vendtagain 10-01-2009 12:26 AM

change the $PATH variable and make /bin come before the /usr/local/bin

ArfaSmif 10-01-2009 12:50 AM

Q1: don't know

Q2: mv /bin/grep /bin/grep.old

Q3; ln -s /usr/local/bin/grep /bin/grep (after you have done Q2 above. Make sure it is has permissions as the original grep had. (usually -rwxr-xr-x. 1 root root 89964 2009-02-25 11:04 /bin/grep)

chrism01 10-01-2009 01:07 AM

You've had some good answers for q2, q3.
Re q1; you can't; yum only recognises stuff installed via yum (actually rpm under the covers).
See rpm -V

Kenhelm 10-01-2009 01:12 AM

Your user shell may have hashed the location of the old grep command and this could be why it doesn't find the new version.
'hash' is a bash builtin command. As the shell finds commands along the search path it remembers the found location in an internal hash table. The next time you enter a command the shell uses the value in its hash table instead of searching the path again.
Code:

# To see your 'hash' table:-
hash

# If 'grep' is on the hash table you can delete it with:-
hash -d grep


lutusp 10-01-2009 01:27 AM

Quote:

Originally Posted by Andrew Dufresne (Post 3702756)
Hi,

I have got two versions of grep on my system.

grep 2.5.1 which is in /bin
grep 2.5.4 which is in /usr/local/bin . I complied it from source code.

Question 1
Now, 'yum info grep' still shows the older 2.5.1a version. How can I tell yum that a new version of grep in on board now?

Question 2
How can I remove the old grep? I tried using 'yum remove grep' but then it showed a huge list that too will be removed with grep so I didn't pursue this way. What will be the proper way?

Question 3
When I run grep as a root, it uses 2.5.4. Although /bin and /usr/local/bin both are in root's $PATH.
But when I run grep as user, it uses 2.5.1. Here too /bin and /usr/local/bin both are in its $PATH.

What is the reason of this different behaviour?

How can I ensure that every user gets grep 2.5.4 without using its absolute path?

Regards

The solution is to make a shortcut in /usr/local/bin to the grep version you want to use. Typically, /usr/local/bin is searched first in a path search, so this location get priority.

1. Move the grep version located in /usr/local/bin somewhere else.

2. symlink from /usr/local/bin to the desired version.

Quote:

grep 2.5.4 which is in /usr/local/bin . I complied it from source code.
Can I ask you something? Why did you do do that? What was wrong with the provided grep version? Are you aware that you could have compiled it so it overwrote the version in /bin? And that you can still move the copy in /usr/local/bin to /bin?

Andrew Dufresne 10-01-2009 01:34 PM

Thanks to all you guys for replying to my queries so promptly.

Quote:

Originally Posted by Andrew Dufresne (Post 3702756)
Question 1
Now, 'yum info grep' still shows the older 2.5.1a version. How can I tell yum that a new version of grep in on board now?

Quote:

Originally Posted by chrism01 (Post 3702896)
Re q1; you can't; yum only recognises stuff installed via yum (actually rpm under the covers).
See rpm -V

Oh I didn't know that. Thanks for clearing that up to me.

Quote:

Originally Posted by Andrew Dufresne (Post 3702756)
Question 2
How can I remove the old grep? I tried using 'yum remove grep' but then it showed a huge list that too will be removed with grep so I didn't pursue this way. What will be the proper way?

Surely I can rename the old version into grep.old as suggested by ArfaSmif. Actually I asked this question to learn something new. I mean I have always observed that to remove a package I just have to "yum remove package" but in this case the programs getting affected with grep removal were so many that I stopped right there.

Now, it seems that is really not easy to remove grep. No big deal, I can live with that. But if somebody as any idea about it then please share.

Quote:

Originally Posted by Andrew Dufresne (Post 3702756)
Question 3
When I run grep as a root, it uses 2.5.4. Although /bin and /usr/local/bin both are in root's $PATH. But when I run grep as user, it uses 2.5.1. Here too /bin and /usr/local/bin both are in its $PATH. What is the reason of this different behaviour? How can I ensure that every user gets grep 2.5.4 without using its absolute path?

Quote:

Originally Posted by Kenhelm (Post 3702905)
Your user shell may have hashed the location of the old grep command and this could be why it doesn't find the new version.
'hash' is a bash builtin command. As the shell finds commands along the search path it remembers the found location in an internal hash table. The next time you enter a command the shell uses the value in its hash table instead of searching the path again.
Code:

# To see your 'hash' table:-
hash

# If 'grep' is on the hash table you can delete it with:-
hash -d grep


Thanks a lot Kenhelm. It solved my problem. This is what I love about Linux, you learn something new almost everyday.

Andrew Dufresne 10-01-2009 01:45 PM

Quote:

Originally Posted by lutusp (Post 3702927)
The solution is to make a shortcut in /usr/local/bin to the grep version you want to use. Typically, /usr/local/bin is searched first in a path search, so this location get priority.

1. Move the grep version located in /usr/local/bin somewhere else.

2. symlink from /usr/local/bin to the desired version.

Thanks for your suggestions.

Quote:

Originally Posted by lutusp (Post 3702927)
Can I ask you something? Why did you do do that? What was wrong with the provided grep version? Are you aware that you could have compiled it so it overwrote the version in /bin? And that you can still move the copy in /usr/local/bin to /bin?

Actually grep 2.5.1 seems to have a bug in it. Please see this thread for details.

Quote:

Originally Posted by lutusp (Post 3702927)
Are you aware that you could have compiled it so it overwrote the version in /bin?

Really? How? Please enlighten me in this regard.

The INSTALL help with source code says
Code:

./configure;  make ; make check ; make install
so I did it. Which command should I add here to overwrite the previous version?

Moreover, old version of grep was installed with yum, so I don't think a compilation can affect previous version in anyway. If you have any idea then please share?

Quote:

Originally Posted by lutusp (Post 3702927)
And that you can still move the copy in /usr/local/bin to /bin?

Sure I can. But I try my best to avoid issuing any command as root or changing environmental variables. That's why I was looking for other ways.

Regards


All times are GMT -5. The time now is 07:25 AM.