到目前为止,我可以使用以下命令从Ubuntu上的单个文件夹中读取文件:

for i in /path/to/files/Folder1/*.pcd
  do
    if [ ! -z $last_i ]
  then
    ./vapp $last_i $i
  fi
  last_i="$i"
done


这将读取Folder1中的所有文件。我也有文件夹2和3(即Folder2,Folder3)。每个文件夹中有几个100个文件,它们的编号都很简单,例如0000.pcd,0001.pcd ... 0129.pcd ...等。

我尝试使用

/path/to/files/Folder{1..3}/*.pcd


问题在于,它现在要从一个文件夹中取出所有文件并处理其中的两个文件,然后再以相同的方式遍历该文件夹中的所有文件,然后再移至下一个文件夹。

我真正想要的是从我的三个文件夹中的每一个获取第i个文件名,例如000i.pcd并将其(包括路径)传递到我的应用程序以进行一些计算。

实际上,我想这样做:

./vapp /Folder1/000i.pcd /Folder2/000i.pcd /Folder3/000i.pcd

最佳答案

单独使用本机bash功能及其扩展的glob功能。从/path/to/files/运行脚本

#!/bin/bash

shopt -s globstar nullglob dotglob

i=0
end=129
while [ "$i" -le "$end" ]
do

    # Generating the file-name to be generated, the numbers 0-129
    # with 4 character padding, taken care by printf

    file="$(printf "%04d.pcd" $i)"

    # The ** pattern enabled by globstar matches 0 or more directories,
    # allowing the pattern to match to an arbitrary depth in the current
    # directory.

    fileList=( **/"$file" )

    # Remember to un-comment the below line and see if the required files
    # are seen by doing
    # printf "%s\n" "${fileList[@]}"
    # and run the executable below

    ./vapp "$(printf "%s " "${fileList[@]}")"

done


extglob功能的使用已从该wonderful answer中重新使用。

关于bash - 在bash脚本中从具有相同文件名的不同文件夹加载多个文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41673132/

10-13 09:00
查看更多