LearningSlowly
LearningSlowly

Reputation: 9431

Bash script - inputting multiple files to function

I have a series of folders containing multiple files:

-Scenario_1
---- file_1.txt
---- file_2.txt
---- file_3.txt
-Scenario_2
---- file_1.txt
---- file_2.txt
---- file_3.txt

I wish to iterate over each folder, iterate through the files and apply a function to all of these files

build () {
    for folder in ./scenarios/*; do

        echo "Working on" $folder

        `function` (space separated list of files in the folder) -o $folder/output.txt

    done



}

build

As an example command, it would look like this: function file_1.txt file_2.txt file_3.txt -o $folder/output.txt

I've tried the following:

`$(find . iname $folder/*.txt)

Upvotes: 0

Views: 255

Answers (2)

em110905
em110905

Reputation: 9

It seems you want to create a list of files space seperated, 'ls -1' will do this, and you can specify the criteria of the search i.e. .txt files. Then you have the list of files to use in your function.

build
{
   for folder in ./scenarios/*
   do
      filelist=`ls -1 *.txt`
      `function` $filelist ....
   done
}

Upvotes: -1

simonz
simonz

Reputation: 191

find -iname only matches on basenames (leading directories for any files considered for a match are removed), so your expression never matches anything. Use -ipath instead.

Or, if you know these folders are never empty and don't contain subdirectories, simple globbing is good enough. There are plenty of variants, e.g. this should work:

for folder in ./scenarios/*; do
  (cd $folder && function * -o output.txt)
done

Upvotes: 2

Related Questions