Grep is not working

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read how to get help
Post Reply
talacasto
Level 1
Level 1
Posts: 23
Joined: Tue Mar 30, 2010 11:21 am

Grep is not working

Post 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,

User avatar
viking777
Level 14
Level 14
Posts: 5115
Joined: Mon Dec 01, 2008 11:21 am

Re: Grep is not working

Post 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?)
Fujitsu Lifebook AH532. Intel i5 processor, 6Gb ram, Intel HD3000 graphics, Intel Audio/wifi. Realtek RTL8111/8168B Ethernet.Lubuntu 13.10,Ubuntu12.10 (Unity), Mint16 (Cinnamon), Manjaro (Xfce).

User avatar
RedWagon
Level 5
Level 5
Posts: 632
Joined: Tue May 26, 2009 6:41 pm

Re: Grep is not working

Post 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.

talacasto
Level 1
Level 1
Posts: 23
Joined: Tue Mar 30, 2010 11:21 am

Re: Grep is not working

Post 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,

User avatar
RedWagon
Level 5
Level 5
Posts: 632
Joined: Tue May 26, 2009 6:41 pm

Re: Grep is not working

Post 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.

talacasto
Level 1
Level 1
Posts: 23
Joined: Tue Mar 30, 2010 11:21 am

Re: Grep is not working

Post 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...

:(

BrianD
Level 4
Level 4
Posts: 313
Joined: Fri Feb 12, 2010 12:22 am
Location: Tallahassee, Florida, USA

Re: Grep is not working

Post 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"
need I say more??

User avatar
DrHu
Level 17
Level 17
Posts: 7525
Joined: Wed Jun 17, 2009 8:20 pm

Re: Grep is not working

Post 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

User avatar
Kaye
Level 5
Level 5
Posts: 933
Joined: Fri Feb 06, 2009 5:05 pm
Location: Boston College
Contact:

Re: Grep is not working

Post 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'.
"In somnis veritas"
Antivirus or defragging?
Image

talacasto
Level 1
Level 1
Posts: 23
Joined: Tue Mar 30, 2010 11:21 am

Re: Grep is not working

Post 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

User avatar
RedWagon
Level 5
Level 5
Posts: 632
Joined: Tue May 26, 2009 6:41 pm

Re: Grep is not working

Post 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

BrianD
Level 4
Level 4
Posts: 313
Joined: Fri Feb 12, 2010 12:22 am
Location: Tallahassee, Florida, USA

Re: Grep is not working

Post 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 ~ ] $
need I say more??

User avatar
RedWagon
Level 5
Level 5
Posts: 632
Joined: Tue May 26, 2009 6:41 pm

Re: Grep is not working

Post 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...

BrianD
Level 4
Level 4
Posts: 313
Joined: Fri Feb 12, 2010 12:22 am
Location: Tallahassee, Florida, USA

Re: Grep is not working

Post 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.
need I say more??

User avatar
RedWagon
Level 5
Level 5
Posts: 632
Joined: Tue May 26, 2009 6:41 pm

Re: Grep is not working

Post 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.

BrianD
Level 4
Level 4
Posts: 313
Joined: Fri Feb 12, 2010 12:22 am
Location: Tallahassee, Florida, USA

Re: Grep is not working

Post 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"
need I say more??

ismitley
Level 1
Level 1
Posts: 1
Joined: Thu Apr 01, 2010 5:13 pm

Re: Grep is not working

Post 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

Post Reply

Return to “Newbie Questions”