shell脚本grep命令-c(count) - 命令行排序是否重要?
只是一个关于疑问的快速问题。这两个区别有什么区别:
grep "$genre" "$i" | grep "$type" -c
他们也许
grep "$genre" "$i" | grep -c "$type"
做同样的事情?
just a quick question regarding a doubt. What's the difference between these two:
grep "$genre" "$i" | grep "$type" -c
and
grep "$genre" "$i" | grep -c "$type"
Do they do perhaps the same thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
grep“ $ type” -c
的POSIX标准标准行为,假设$ type
不会扩展到以仪表率开头的字符串,是对待-c
作为文件名。只有
grep
的非标准版本(例如GNU项目构建的版本以及在Linux系统上通常找到的版本)将把-c
视为启用“计数”行为的选项不是在第一个位置论点之前。编写您的脚本以需要非标准工具是不好的做法,除非它们从这些工具中获得一定的具体收益。使用
GREP -C“ $ TYPE”
。The POSIX standard-mandated behavior for
grep "$type" -c
, presuming that$type
does not expand to a string starting with a dash, is to treat-c
as a filename.Only nonstandard versions of
grep
(such as the one built by the GNU project and commonly found on Linux systems) will treat-c
as an option enabling "count" behavior when it isn't before the first positional argument.It's bad practice to write your scripts to require nonstandard tools unless they gain some concrete benefit from those tools. Use
grep -c "$type"
.如您从
man grep
中看到的,-c
交换机属于所谓的“常规输出控制”。这些开关可以放在grep ...
命令中的不同位置,这对一般结果没有影响,因此,实际上,您提到的这两条线都是相等的。As you can see from
man grep
, the-c
switch belongs to the so-called "General Output Control". Those switches can be placed on different places in thegrep ...
command and this has no impact on the general outcome, so indeed, both lines you mention are equal.