Linux 通配符行为不正确
我将运行具有以下命名约定的进程列表:
a_feed
b_feed
c_feed
...
我编写了一个 bash shell 脚本,它允许我过滤掉具有这些命名模式的进程。请看下面我的 shell 脚本中的一行:
ps -ef | grep -i *_feed | grep -v grep | awk '{print $2, " ", $8, " ", $10}'
由于某种原因,grep -i *_feed
无法找到任何符合模式 *_feed
的进程。
有人知道为什么吗?
谢谢。
I will be running a list of processes that will have the following naming conventions:
a_feed
b_feed
c_feed
...
I have written a bash shell script that will allow me to filter out the processes with these naming patterns. Please look at the one line in my shell script below:
ps -ef | grep -i *_feed | grep -v grep | awk '{print $2, " ", $8, " ", $10}'
For some reason, grep -i *_feed
is unable to find any process that conforms to the pattern *_feed
.
Does anyone have any ideas why?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
grep 使用正则表达式,其中 * 表示
匹配 0 次或多次
,而不是任何字符
。您应该将其替换为
grep -i .*_feed
grep users regular expression, in which * means
matches 0 or more times
, and notany character
.You should replace it with
grep -i .*_feed
*
前面需要一些东西。另外,如果您的工作目录中有一个带有
*_feed
模式的文件,bash 将进行通配符扩展。使用:
*
need something in front of it.Also, if you have a file with the pattern
*_feed
in your working directory bash will do wildcard expansion.Use:
来自 grep 手册页:
因此,默认情况下模式将是正则表达式。
在您的示例中,您可以使用
grep -i ".*_feed"
from grep man page:
so, by default the pattern would be regular expression.
in your example, you could use
grep -i ".*_feed"
我通常将进程列表的输出保存在 shell 中
变量,然后搜索匹配的行作为新命令。这避免了需要 grep -v
删除正在运行的 grep 命令。
我还匹配了
awk
中的行,因此根本不需要grep
。我认为这更容易阅读和理解:I usually save the output of the list of processes in a shell
variable and then search for the matching lines as a new command. This avoids needing a
grep -v
to remove the running
grep
command.I also match the lines inside
awk
so that nogrep
is needed at all. I think this is easier to read and understand: