c++ - 以目录作为参数运行 C++ 文件?使用 Bash 代替?

标签 c++ bash

我有一个以图像作为参数的 C++ 文件。我有一个目录,我需要在上面执行脚本。看起来像:

- parent
-- sub0
--- 0.pgm
--- 1.pgm
--- ...
-- sub1
--- 0.pgm
--- 1.pgm
--- ...
-- ...

parent包含120个子文件夹,每个子文件夹包含100-200个.pgm文件。 C++ 文件将 .pgm 作为参数。

与其运行 file.cpp filename.pgm 数千次,有没有办法在 C++ 中将文件夹作为参数?理想情况下,我想:

  1. 在父文件夹上调用 file.cpp,它递归地处理子文件夹和文件
  2. 改用 Bash 来做

使用 Bash,一些伪代码:

g++ -march=native -O2 file.cpp -o output
for $filename in folder
    ./output $filename
endfor

我该如何实现?

最佳答案

for 循环遍历字符串列表,而不是目录的内容。您需要一个 glob 来扩展到您想要的文件列表。

shopt -s globstar
for filename in folder/**/*.pgm; do
   ./output "$filename"
done

关于c++ - 以目录作为参数运行 C++ 文件?使用 Bash 代替?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42631040/

相关文章:

c++ - std::thread 使用 gcc-linaro-4.9.4 在 Raspbian 中导致段错误

c++ - 实现 unique_ptr : Deleting non-allocated objects

bash - `-s --` 标志对 npm 有什么作用?

linux - 显示基于 "Windows-speak"的兼容命令的 bash 脚本

regex - 根据第二个文本文件从文本文件中删除重复项

bash - 在/etc/fstab 中挂载一个 Windows 隐藏共享。我如何逃避 $

C++调用时指定函数的非模板版本

c++ - 构建最大的堆叠塔与集团和限制

Git 错误远程 : Empty password

c++ - 带有fedora 23的Opencv Qt:程序意外完成