【问题标题】:load multiple files from different folders with same file name in bash-scipt在 bash-script 中从具有相同文件名的不同文件夹加载多个文件
【发布时间】:2017-01-16 09:31:06
【问题描述】:

到目前为止,我可以使用以下方法从我的 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)。每个文件夹里面有几百个文件,简单编号如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

【问题讨论】:

  • 您将需要嵌套循环,例如(for i in /path/to/Folders{1..3}; do for j in "$i"/*; do ... stuff on "$j"...; done; done)

标签: bash ubuntu


【解决方案1】:

单独使用原生 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 中重复使用。

【讨论】:

    【解决方案2】:

    我昨天以与 Inian 建议的方式类似的方式解决了我的问题。除了我的方式是手动方式! 它对我有用,但我同意 Inian 的方式更加灵活。无论如何,这是我的解决方案:

    j=0
    leadingZeros1="00000000"
    leadingZeros2="0000000"
    leadingZeros3="000000"
    
    fol0="lidar03_00/"
    fol1="lidar03_01/"
    fol2="lidar03_02/"
    ext=".pcd"
    basepath="/my/path/"
    
    for i in /my/path/lidar03_00/*.pcd
    do
    
    if [ ! -z $last_i ]
    ((j+=1))
    then   
    
        if [ $j -le 9 ]   
    then       
        mypath1=$basepath$fol0$leadingZeros1$j$ext
        mypath2=$basepath$fol1$leadingZeros1$j$ext
        mypath3=$basepath$fol2$leadingZeros1$j$ext       
        fi
        sleep .009
    
        if [ $j -ge 10 ]
        then
      if [ $j -le 99 ]
      then       
      mypath1=$basepath$fol0$leadingZeros2$j$ext
      mypath2=$basepath$fol1$leadingZeros2$j$ext
      mypath3=$basepath$fol2$leadingZeros2$j$ext     
      fi
    
      if [ $j -ge 100 ]
      then
        if [ $j -le 999 ]; then       
        mypath1=$basepath$fol0$leadingZeros3$j$ext
        mypath2=$basepath$fol1$leadingZeros3$j$ext
        mypath3=$basepath$fol2$leadingZeros3$j$ext
    
        fi;
      fi
    
    fi
    #echo $mypath1
    #echo $mypath2
    #echo $mypath3
    
     ./vapp -i $mypath1 $mypath2 $mypath3 .txt"    
    fi
    last_i="$i"
    done
    

    我承认,这不是最好的解决方案,但它现在解决了我的问题。如果我需要再次这样做,我可能会按照 Inian 的方式进行。 谢谢大家的帮助。 我试图避免使用逻辑 AND (&&) 的嵌套 if,但不知何故它不起作用,我不想花更多时间在这上面。因此笨拙的方式......

    【讨论】:

      猜你喜欢
      • 2017-06-18
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多