Page 1 of 1

Grep is not working

Posted: Tue Mar 30, 2010 12:25 pm
by talacasto
Hi team,

Driving crazy with a simple question:

Code: Select all

ps -ef | grep skype
 grep: command not found
Command not found???

What I'm missing here?

Thanks in advance,

Re: Grep is not working

Posted: Tue Mar 30, 2010 1:49 pm
by viking777
What I'm missing here?
I guess you are missing grep (though I don't know why).

Run

Code: Select all

apt-cache policy grep
to see if it is installed.

If it isn't you will know what to do (I suppose?)

Re: Grep is not working

Posted: Tue Mar 30, 2010 3:29 pm
by RedWagon
run

Code: Select all

echo $PATH
and paste back the output. $PATH tells the shell where to look for commands and if you accidentally clear it you won't be able to run anything. My path looks like this:

Code: Select all

verdow@w1-techcage-02 ~ $ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Try running this:

Code: Select all

ps -ef | /bin/grep skype
This gives the shell an absolute path to grep, if it is your $PATH variable then this will work. If this doesn't work, something else is wrong.

Re: Grep is not working

Posted: Tue Mar 30, 2010 6:56 pm
by talacasto
Hi team,

Thanks for your answers.

I made this tests:

apt-cache policy grep
grep:
Installed: 2.5.4-4
Candidate: 2.5.4-4
Version table:
*** 2.5.4-4 0
500 http://archive.ubuntu.com karmic/main Packages
100 /var/lib/dpkg/status


renton@clockwork ~ $ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

renton@clockwork ~ $ ps -ef | /bin/grep skype
renton 20585 20282 0 00:59 pts/0 00:00:00 /bin/grep skype


This is a new Linux Mint distro without any packages removed; even grep!



Seems it works sometimes:

ps -ef | grep firefox
renton 2495 1 23 Mar30 ? 00:23:34 /usr/lib/firefox-3.5.8/firefox
renton 20787 20282 0 01:00 pts/0 00:00:00 grep --colour=auto firefox


And fails others:

aptitude search sky | grep spanish
 grep: command not found


Let me know. Thanks,

Re: Grep is not working

Posted: Tue Mar 30, 2010 7:37 pm
by RedWagon
are you switching users or using different terminal sessions when it doesn't work? Grep is there at /bin/grep so the problem is most likely with the shell finding grep and not the installation of grep itself. Do you have a weird partition table? Also run echo $PATH right after it doesn't work to see if your PATH is being changed by something.

Re: Grep is not working

Posted: Wed Mar 31, 2010 2:26 am
by talacasto
RedWagon wrote:are you switching users or using different terminal sessions when it doesn't work? Grep is there at /bin/grep so the problem is most likely with the shell finding grep and not the installation of grep itself. Do you have a weird partition table? Also run echo $PATH right after it doesn't work to see if your PATH is being changed by something.
No, it's with the same user and session...

:(

Re: Grep is not working

Posted: Wed Mar 31, 2010 8:17 am
by BrianD
umm...

if the report you're getting back says

grep: command not found

that would indicate that the program `grep` is reporting "command not found"

Re: Grep is not working

Posted: Wed Mar 31, 2010 2:06 pm
by DrHu
Instead of trying to fix a compound command, just use grep itself: does it then work or not ?
In a terminal, check
  • grep
    --your response is ?
  • /bin/grep
    --your response is ?
If you get a response, then YES grep is working and there is something else incorrect, even though it looks fine from your code display |grep

Even though I don't have skype installed, if i cut and past your command I do get a response
  • ps -ef | grep skype
    MintuserID 6016 3445 0 14:13 pts/0 00:00:00 grep --colour=auto skype
--whether or not there is none or more spaces before the grep command

Re: Grep is not working

Posted: Wed Mar 31, 2010 2:54 pm
by Kaye
BrianD wrote:umm...

if the report you're getting back says

grep: command not found

that would indicate that the program `grep` is reporting "command not found"
No, it means that the terminal session can't find the command 'grep'.

Re: Grep is not working

Posted: Thu Apr 01, 2010 4:52 am
by talacasto
DrHu wrote:Instead of trying to fix a compound command, just use grep itself: does it then work or not ?
In a terminal, check
  • grep
    --your response is ?
  • /bin/grep
    --your response is ?
If you get a response, then YES grep is working and there is something else incorrect, even though it looks fine from your code display |grep

Even though I don't have skype installed, if i cut and past your command I do get a response
  • ps -ef | grep skype
    MintuserID 6016 3445 0 14:13 pts/0 00:00:00 grep --colour=auto skype
--whether or not there is none or more spaces before the grep command
Thanks for your answeres team.

Tests:

Code: Select all

$ grep
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

$ /bin/grep
Usage: /bin/grep [OPTION]... PATTERN [FILE]...
Try `/bin/grep --help' for more information.

Code: Select all

$ ps -ef | grep skype
renton    7905  7782  0 10:53 pts/0    00:00:00 grep --colour=auto skype
Maybe a problem combining with SUDO?
$ sudo ps -ef | grep skype
 grep: command not found

Re: Grep is not working

Posted: Thu Apr 01, 2010 3:05 pm
by RedWagon
Maybe a problem combining with SUDO?
Using sudo would be running that command as a different user, root.

There's no reason to run that command as root. Using root with these commands won't hurt anything, but if you keep arbitrarily running stuff as root you have a much higher chance of breaking something. Root has a different path than other users, and when you use sudo you're using root's $PATH to find commands. However, running

Code: Select all

sudo echo $PATH
will display the path of the user running sudo and not root's which is weird. Paste back root's path by running

Code: Select all

su
echo $PATH
exit

Re: Grep is not working

Posted: Thu Apr 01, 2010 3:43 pm
by BrianD
RedWagon wrote:
Maybe a problem combining with SUDO?
Using sudo would be running that command as a different user, root.

There's no reason to run that command as root. Using root with these commands won't hurt anything, but if you keep arbitrarily running stuff as root you have a much higher chance of breaking something. Root has a different path than other users, and when you use sudo you're using root's $PATH to find commands. However, running

Code: Select all

sudo echo $PATH
will display the path of the user running sudo and not root's which is weird. Paste back root's path by running

Code: Select all

su
echo $PATH
exit
...actually, both of those should display the same information. if you invoke "su" by itself, it will su to root and keep the current environment. to su to root and use root's environment, you should invoke it with

Code: Select all

su -
echo $PATH
exit
here's a sample output:


[brian@astro ~ ] $ whoami
brian
[brian@astro ~ ] $ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
[brian@astro ~ ] $ sudo echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
[brian@astro ~ ] $ su
Password:
[astro /home/brian ] # echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
[astro /home/brian ] # exit
exit
[brian@astro ~ ] $ su -
Password:
[astro ~ ] # echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[astro ~ ] # exit
logout
[brian@astro ~ ] $

Re: Grep is not working

Posted: Thu Apr 01, 2010 4:07 pm
by RedWagon
...actually, both of those should display the same information. if you invoke "su" by itself, it will su to root and keep the current environment. to su to root and use root's environment, you should invoke it with
Well, you're right about su - showing a different path but check this out:

Code: Select all

verdow@w1-techcage-02 ~ $ echo $PATH
/home/verdow/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
verdow@w1-techcage-02 ~ $ sudo echo $PATH
/home/verdow/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
verdow@w1-techcage-02 ~ $ su
Password: 
w1-techcage-02 verdow # echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
w1-techcage-02 verdow # exit
exit
verdow@w1-techcage-02 ~ $ su -
Password: 
w1-techcage-02 ~ # echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
w1-techcage-02 ~ # exit
logout
verdow@w1-techcage-02 ~ $ 
Apparently I have three paths: mine, root's and a weird in between one.

EDIT: Tried export PATH and that still didn't change anything...

Re: Grep is not working

Posted: Thu Apr 01, 2010 4:50 pm
by BrianD
hmm. not that strange, I don't think.

if you su, you gain root using the user environment

if you su -, you gain root using the root environment

I think the difference in the $PATH that you're noticing otherwise, between user and root, is due to changes/additions performed during .login, .profile, .bash_profile, etc. note that the "user" environment variable includes /usr/verdow/.bin (probably added during user login), "su" doesn't include that, as it didn't 'log in' as the user, it just inherited the environment, and "su -" doesn't include it as it didn't log in as the user, and used root's environment.

Re: Grep is not working

Posted: Thu Apr 01, 2010 4:58 pm
by RedWagon
how would I add /home/verdow/.bin to the user environment so that su will see it? It's really handy having my scripts in my home directory, but none of them can be run as root until I create a symbolic link from ~/.bin/script to /usr/local/bin/script

I know this tread has become sidetracked, but we're still here to hep with the grep issue talacasto.

Re: Grep is not working

Posted: Thu Apr 01, 2010 5:05 pm
by BrianD
RedWagon wrote:how would I add /home/verdow/.bin to the user environment so that su will see it? It's really handy having my scripts in my home directory, but none of them can be run as root until I create a symbolic link from ~/.bin/script to /usr/local/bin/script
hint given, previously. :wink:

likely, that user path addition exists in /home/verdow/.bashrc ...you can add the same absolute path to /root/.bashrc :D
... or in the appropriate .profile, or wherever. the syntax is:

PATH="$HOME/.bin:$PATH"

...and that is probably what it says now in your .bashrc, .login, or .profile in your user home directory. to add that user bin directory to root's $PATH, you'd add

PATH="/home/verdow/.bin:$PATH"

..to the .bashrc, .login, or .profile in /root (which is root's home directory)
RedWagon wrote: I know this tread has become sidetracked, but we're still here to hep with the grep issue talacasto.
I have no idea what's happening with that... it's just "strange"

Re: Grep is not working

Posted: Thu Apr 01, 2010 5:14 pm
by ismitley
Taken from "man su"

The current environment is passed to the new shell. The value of $PATH
is reset to /bin:/usr/bin for normal users, or
/sbin:/bin:/usr/sbin:/usr/bin for the superuser. This may be changed
with the ENV_PATH and ENV_SUPATH definitions in /etc/login.defs.

So RedWagon: Set your ENV_SUPATH to include /home/verdow/.bin or whatever, if you want it in your su path.

- Isaac