English 中文(简体)
我怎么能对"wc -l"的结果做数学操作?
原标题:How can I do a math operation to the result of "wc -l"?
  • 时间:2024-05-18 21:39:25
  •  标签:
  • bash

我想检查文件夹中的文件数量, 我希望结果, 例如, 乘以 4 。 我尝试了类似的东西 :

cd path/to/my/folder && find . -maxdepth 1 -type f | wc -l | expr {} * 4

但它不起作用,它一直工作到:

cd path/to/my/folder && find . -maxdepth 1 -type f | wc -l

但当我添加类似 "expr * * * * 4" 的东西时, 我有一个错误... 我还尝试了其它替代的“ express”, 但至今为止没有运气 。

BTW,我还想让结果对另一个程序“可操作性”, 比如 ffmpeg, 比如:

cd path/to/my/folder && find . -maxdepth 1 -type f | wc -l | expr {} * 4 | ffmpeg -i {} out.mp4

以上是简化我所要做的事的一个愚蠢的例子...对于那些好奇的人,我使用>>https://github.com/nihui/rife-ncnn-vulkan 来将视频的框架/框架翻四番,所以我需要检查文件夹中的文件数量(图像),自动将数字乘以4,并将结果纳入流行-ncnn-vulkan的“n”参数。

ffmpeg -i "video.mkv" -r 12 -c:v libwebp -lossless 1 -y input_frames/frame_%06d.webp && cd input_frames && find . -maxdepth 1 -type f | wc -l | expr {} * 4 | rife-ncnn-vulkan -i input_frames -o output_frames -f %08d.webp -x -v -n {}

(注意结尾处的"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

问题回答

这在Bash和Zsh中有效:

cd path/to/my/folder
let count="$(find . -type f -maxdepth 1 | wc -l) * 4"
rife-ncnn-vulkan ... -n $count

let 命令允许 shell 进行算术 。

Update

感谢查尔斯・达菲的建议

count="$(( $(find . -type f -maxdepth 1 | wc -l) * 4 ))"

不取代输入, 它只是一个字符串, 将字面上传递为参数。 但是, 您可以使用 < code> alxargs 来捕捉输入并将其传递为参数 。 您可以使用类似 < code>- I/code > 的东西来让输入值使用 (或其他您碰巧喜欢的字符串) 的占位符, 比如 :

find . -maxdepth 1 -type f | wc -l | xargs -I{} expr {} "*" 4

或者不要使用占位符, 这样它就会在命令结尾处添加捕获的输入, 所以您需要重新排列表达式 :

find . -maxdepth 1 -type f | wc -l | xargs expr 4 "*"

请注意,无论哪种情况,你都必须 必须 引用或跳出 ,否则外壳会将其扩展为目录中的文件名列表,而不是作为字面星号传递。

展示如何运用海武对问题所显示的代码的回答:

ffmpeg 
  -i "video.mkv" 
  -r 12 
  -c:v libwebp 
  -lossless 1 
  -y input_frames/frame_%06d.webp || exit

count=$(( $(find input_frames -maxdepth 1 -type f -print | wc -l) * 4 )) || exit

rife-ncnn-vulkan -x -v 
  -i input_frames 
  -o output_frames 
  -f %08d.webp 
  -n "$count" || exit

通知 :

  • Instead of combining all our commands with &&, we re ending them with || exit; this has the same effect but is more maintainable.
  • We re not piping commands together. Pipes aren t needed for this, except between find and wc (and some versions of find have a -count action; if we used that, we wouldn t need wc either).
  • We re not running cd input_frames. If we did that it would change the directory the second copy of ffmpeg runs in, but you only want to change the directory find operates against -- we can do that by just changing . to input_frames.
  • We re storing count in a variable and using that variable later, not piping the value around.
  • Whitespace does a great deal to help readability.




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...