加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell – 为什么wc实用程序会生成多行“total”?

发布时间:2020-12-15 21:56:11 所属栏目:安全 来源:网络整理
导读:我在从Cygwin运行的 shell脚本中使用wc实用程序,我注意到输出中有多行“total”. 以下函数用于计算源文件中的行数: count_curdir_src() { find . '(' -name '*.vb' -o -name '*.cs' ')' -a '!' -iname '*.Designer.*' -a '!' -iname '.svn' -print0 | x
我在从Cygwin运行的 shell脚本中使用wc实用程序,我注意到输出中有多行“total”.

以下函数用于计算源文件中的行数:

count_curdir_src() {
    find . '(' -name '*.vb' -o -name '*.cs' ')' 
        -a '!' -iname '*.Designer.*' -a '!' -iname '.svn' -print0 | 
    xargs -0 wc -l
}

但它对某个目录的输出如下所示:

$find . '(' -name '*.vb' -o -name '*.cs' ')' -a '!' -iname '*.Designer.*' -a '!' -iname '.svn' -print0 | xargs -0 wc -l
     19 ./dirA/fileABC.cs
    640 ./dirA/subdir1/fileDEF.cs
    507 ./dirA/subdir1/fileGHI.cs
   2596 ./dirA/subdir1/fileJKL.cs
(...many others...)
     58 ./dirB/fileMNO.cs
     36 ./dirB/subdir1/filePQR.cs
 122200 total
  6022 ./dirB/subdir2/subsubdir/fileSTU.cs
    24 ./dirC/fileVWX.cs
(...)
    36 ./dirZ/Properties/AssemblyInfo.cs
    88 ./dirZ/fileYZ.cs
 25236 total

看起来wc会在过程中的某个位置重置.它不能由文件名或目录名中的空格字符引起,因为我使用-print0选项.它只发生在我最大的源树上运行时.

那么,这是wc或Cygwin中的错误吗?或者是其他东西? wc联机帮助页说:

Print newline,word,and byte counts
for each FILE,and a total line if
more than one FILE is specified.

它没有提到任何关于多个总线数(中间总计数或其他东西),所以谁应该责怪这里?

解决方法

你多次调用wc – 对于xargs提供的每个“批量”输入参数一次.你每批获得一个.

另一种方法是使用临时文件和wc的–files0-from选项:

$find . '(' -name '*.vb' -o -name '*.cs' ')' -a '!' -iname '*.Designer.*' -a 
    '!' -iname   '.svn' -print0 > files

$wc --files0-from files

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读