1

So far i am able to read in the files from a single folder on my Ubuntu using:

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

this will read all files in Folder1. I Also have folder 2 and 3 (i.e. Folder2, Folder3). Inside each folder are several 100 files which are simply numbered such as 0000.pcd, 0001.pcd ... 0129.pcd... and so on.

I have tried to use

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

The problem is that it takes now all files from one folder and processes two files within, than goes through all files in this folder the same way before moving on to the next folder.

What i really want is to take from each of my three folders the ith filename e.g. 000i.pcd and pass it (including the path) to my application to do some calculations.

effectively I want to do this:

./vapp /Folder1/000i.pcd /Folder2/000i.pcd /Folder3/000i.pcd
Sven0
  • 23
  • 5

2 Answers2

0

Using native bash features alone, with its extended glob features. Run the script from /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

The usage of extglob features are re-used from this wonderful answer.

Community
  • 1
  • 1
Inian
  • 80,270
  • 14
  • 142
  • 161
0

I have solved my problem yesterday in a similar way to what Inian suggested. Except that my way is the manual way! It works for me but I agree that Inian's way is much more flexible. Here is my solution anyway:

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

I admit, it is not the nicest solution but it fixes my problem for now. If i need to do this again I will probably do it Inian's way. Thanks all for the help. I have tried to avoid the nested ifs using logical AND (&&) but somehow it didn't work and I didn't wanted to spend more time on this. Thus the clumsy way...

Sven0
  • 23
  • 5