3

Grep has been acting strangely on one of my systems (Ubuntu Desktop - all my other systems are Ubuntu Server), and I can't figure out why. I created a control file named text that contains the following text:

grep
test!!

the following commands work on all of my systems except the problem child:

$ grep grep *
text:grep
$ grep 'test!!' *
text:test!!

On my problem child grep simply hangs. I have compared .bashrc, .bash_aliases, and even /etc/bash_completion, but I can't find the problem. Any ideas what could be causing the problem?

6
  • 1
    Does it work if you pass in the path name of the control file rather than * ?
    – SiegeX
    Commented Feb 8, 2011 at 4:30
  • 3
    It might also help if you could post the output of an strace. strace grep grep *
    – phemmer
    Commented Feb 8, 2011 at 4:35
  • Did you try grepping after copying the test file from one of the machine where you did not have this problem? By the way how was the file created? maybe it may have something to do with utf ?!?
    – yasouser
    Commented Feb 8, 2011 at 5:25
  • what does "type grep" say?
    – chris
    Commented Feb 8, 2011 at 8:52
  • SiegeX: It does work, and it also works if I use 'tex*'
    – JohnB
    Commented Feb 8, 2011 at 18:04

3 Answers 3

7

On the problem machine, in the directory you grep 'something' * are there any special files like sockets, named pipes (fifos), etc. there ?

At least in the case of a named pipe, grep won't get an EOF from the named pipe until something actually writes an EOF to the named pipe.

3
  • Thanks for your input - if Patrick doesn't post an answer, you'll get the credit.
    – JohnB
    Commented Feb 8, 2011 at 18:18
  • How would you do a grep -r and exclude named pipes? I've run into this problem too where grep hangs when it gets to a named pipe...
    – Tyler Rick
    Commented Mar 12, 2012 at 18:51
  • I figured it out: Use 'grep --devices=skip -r ...'
    – Tyler Rick
    Commented Mar 12, 2012 at 18:55
4

If it hangs, it sounds to me like it is looking for input from stdin.

You can prove that by typing Control-D (once only) - it will return to the shell.

If this is correct then * does not find any files.

1
  • <Ctrl>-D does not do anything - I need to use <Ctrl>-C to kill grep. I agree though - it sounds like it is looking for input from stdin.
    – JohnB
    Commented Feb 8, 2011 at 17:59
1

Had a similar problem and the culprit was a rouge dir named '-' which was most likely being interpreted as "read from stdin" instead of as a dir name

> grep 'something' * 
> grep 'something' f1 f2 f3 - f4 f5  ...
1
  • Can you describe how to get around this problem without needing to type the names of all the files in the directory by hand? Commented Nov 4, 2017 at 16:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .