ALZ*_*ALZ 6 shell grep regular-expression glob
我有 2 个几乎相同的grep
s:
[Alex@localhost tmp]$ grep /bin/bash /etc/passwd
root:x:0:0:root:/root:/bin/bash
AlexL:x:500:500::/home/AlexL:/bin/bash
user1:x:501:501:user1 12345:/home/user1:/bin/bash
Run Code Online (Sandbox Code Playgroud)
对比
[AlexL@localhost tmp]$ grep /bin/*sh /etc/passwd
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:AlexL:x:500:500::/home/AlexL:/bin/bash
/etc/passwd:user1:x:501:501:user1 12345:/home/user1:/bin/bash
Run Code Online (Sandbox Code Playgroud)
在第二个查询中,我得到了每一行匹配的文件名前缀。我知道要获得相同的结果,我需要将-h
选项放在 2nd 中grep
,但我的问题是:
为什么他们返回不同的输出? 我应该添加更多东西吗?
该任务包括从/etc/passwd
真实用户(无守护进程和系统用户)中检索。
使用:CentOS 6.4, grep gnu 2.6.3 版本
ang*_*gus 11
你的外壳会扩展/bin/*sh
。所以,你真正在做的是
grep /bin/bash /bin/dash /bin/rbash /bin/rzsh /bin/sh /bin/zsh /etc/passwd
Run Code Online (Sandbox Code Playgroud)
也就是说,/bin/bash
在文件/bin/dash
, /bin/rbash
, /bin/rzsh
, /bin/sh
,/bin/zsh
和 中搜索字符串/etc/passwd
。
(与 的输出比较echo /bin/*sh /etc/passwd
。)
由于要搜索多个文件,因此grep
报告它在哪个文件中找到了该字符串。
你想要的是引用你的搜索词,所以它不会被 shell 扩展,并使用适当的正则表达式:
grep '/bin/.*sh' /etc/passwd
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
615 次 |
最近记录: |