bash 如果未指定文件,为什么 grep 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13503753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:50:33  来源:igfitidea点击:

Why doesn't grep work if a file is not specified?

linuxbashshellubuntugrep

提问by AndreaNobili

I have some problem with the Linux grep command, it don't work !!!

我的 Linux grep 命令有问题,它不起作用!!!

I am trying the following test on my Ubuntu system:

我正在我的 Ubuntu 系统上尝试以下测试:

  1. I have create the following folder: /home/andrea/Scrivania/prova

  2. Inside this folder I have created a txt file named prova.txt and inside this file I have write the string testand I have save it

  3. In the shell I have first access the folder /home/andrea/Scrivania/provaand so I have launched the grep command in the following way:

    ~/Scrivania/prova$ grep test
    
  1. 我创建了以下文件夹: /home/andrea/Scrivania/prova

  2. 在这个文件夹中,我创建了一个名为 prova.txt 的 txt 文件,在这个文件中我写了字符串test并保存了它

  3. 在 shell 中,我首先访问了该文件夹/home/andrea/Scrivania/prova,因此我通过以下方式启动了 grep 命令:

    ~/Scrivania/prova$ grep test
    

The problem is that the cursor continues to blink endlessly and cannot find NOTHING! Why? What is the problem?

问题是光标继续无休止地闪烁,什么也找不到!为什么?问题是什么?

回答by Rawkode

You've not provided files for the grep command to scan

您尚未提供文件供 grep 命令扫描

grep "test" *

grep "test" *

or for recursive

或递归

grep -r "test" *

grep -r "test" *

回答by Joshua

Because grep searches standard input if no files are given. Try this.

因为如果没有给出文件,grep 将搜索标准输入。尝试这个。

grep test *

回答by Julien Vivenot

You are not running the command you were looking for.

您没有运行您要查找的命令。

grep test *will look for testin all files in your current directory.

grep test *test在您当前目录中的所有文件中查找。

grep test prova.txtwill look for testspecifically in prova.txt

grep test prova.txttest专门寻找prova.txt

(grep testwill grepthe teststring in stdin, and will not return until EOF.)

(字符串grep test是否会grepteststdin,直到EOF.才会返回。)

回答by themefield

As per GNU Grep 3.0

根据GNU Grep 3.0

A file named -stands for standard input. If no input is specified, grepsearches the working directory .if given a command-line option specifying recursion; otherwise, grep searches standard input.

一个名为的文件-代表标准输入。如果未指定输入, 则在给出指定递归的命令行选项的情况下grep搜索工作目录.;否则,grep 将搜索标准输入。

So for OP's command, without any additional specification, greptries to search in standard input, which is not actually provided there.

因此,对于 OP 的命令,在没有任何额外规范的情况下,grep尝试在标准输入中进行搜索,而标准输入实际上并未在那里提供。

A simple approach is grep -r [pattern], as per the above, to specify recursionwith -rand search in current directory and sub-directories.

一个简单的方法是grep -r [pattern],按照上述,指定递归-r和当前目录和子目录搜索。

Also note that wildcard *only includes files, not directories. If used, a prompt might be shown for hint:

还要注意通配符*只包括文件,包括目录。如果使用,可能会显示提示提示:

grep: [directory_name]: Is a directory

grep: [directory_name]: 是一个目录

回答by Oliver Atkinson

You need to pipe in something to grep - you cant just call grep test without any other arguments as it is actually doing nothing. try grep test *

你需要向 grep 输入一些东西——你不能只调用 grep test 而没有任何其他参数,因为它实际上什么都不做。尝试grep test *

Another use for grep is to pipe in a command

grep 的另一个用途是通过管道输入命令

e.g. This is my home directory:

例如,这是我的主目录:

drwx------+  3 oliver  staff    102 12 Nov 21:57 Desktop
drwx------+ 10 oliver  staff    340 17 Nov 18:34 Documents
drwx------+ 17 oliver  staff    578 20 Nov 18:57 Downloads
drwx------@ 12 oliver  staff    408 13 Nov 20:53 Dropbox
drwx------@ 52 oliver  staff   1768 11 Nov 12:05 Library
drwx------+  3 oliver  staff    102 12 Nov 21:57 Movies
drwx------+  5 oliver  staff    170 17 Nov 10:40 Music
drwx------+  3 oliver  staff    102 20 Nov 19:17 Pictures
drwxr-xr-x+  4 oliver  staff    136 12 Nov 21:57 Public

If i run

如果我跑

l | grep Do

I get the result

我得到了结果

drwx------+ 10 oliver  staff    340 17 Nov 18:34 Documents
drwx------+ 17 oliver  staff    578 20 Nov 18:57 Downloads

remember to pipethe grep command

记得通过管道传递grep 命令

回答by WildCrustacean

From grep man page:

从 grep 手册页:

Grep searches the named input FILEs (or standard input if no files are named, or the file name - is given) for lines containing a match to the given PATTERN.

Grep 搜索命名的输入文件(或标准输入,如果没有文件被命名,或文件名 - 已给出)中包含匹配给定 PATTERN 的行。

If you don't provide file name(s) for it to use, it will try to read from stdin.

如果您不提供要使用的文件名,它将尝试从标准输入读取。

Try grep test *

尝试 grep test *