简体   繁体   English

鱼壳循环计数器

[英]Fish Shell Loop Counter

I'm trying to count pages of PDF documents in a directory - which works fine except I cannot get the counter variable to increase. 我正在尝试对目录中PDF文档的页面进行计数-正常工作,除了无法增加计数器变量。 In the directory are two documents with 1 Page and 4 Pages. 目录中有两个文档,分别为1页和4页。 The return of my below script is: 我下面的脚本的返回是:

1 
4

why isn't it incrementing $i ? 为什么不增加$ i呢?

#!/usr/local/bin/fish


set i 0

for pdf in *.pdf
     set i (math i + (pdfinfo $pdf | grep Pages | awk '{print $2}'))    
     echo $i
end

Another approach: do the math inside awk: 另一种方法:在awk中进行数学运算:

for p in **.pdf; pdfinfo $p; end | awk '$1 == "Pages:" {sum += $2} END {print sum}'

To capture that: 要捕获该信息:

set npages (
    for p in **.pdf
        pdfinfo $p 
    end | awk '$1 == "Pages:" {sum += $2} END {print sum}'
)
echo $npages

Found the issue at hand - whitespace made it problematic... 发现了手头的问题-空白使它成为问题...

By doing **.pdf it gets recursive through all folders... 通过执行**。pdf,它可以遍历所有文件夹...

#!/usr/local/bin/fish


set i 0

for pdf in **.pdf
     set i (math $i+(pdfinfo $pdf | grep Pages | awk '{print $2}')) 
     echo $i
 end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM