Workhorse
Workhorse

Reputation: 1560

Using variable Names in For Loop (Bash)

I have two sets of directories. In the first directory, I have 8 files as such:

file1.txt file2.txt file3.txt ... file8.txt

In the second directory called output, I have multiple subdirectories. I want to write for loop that goes into each sub-directory inside output (so they are directory1, directory2 etc...) and reads a file called database_file_#.txt. Currently, I have written this loop below, but it only works for one directory at a time.

for file in *; do 
name="${file%%.*}"
python script.py --parameter $file --out ../../output/directory1/${cts_name} 
--readin ../../output/directory1/database_file_1.txt 
done

For the second directory, I do the following:

 for file in *; do 
name="${file%%.*}"
python script.py --parameter $file --out ../../output/directory1/${cts_name} 
--readin ../../output/directory2/database_file_2.txt 
done

I do not want to keep re-writing directory# for all the directories inside output. Is there a way I can use a variable instead?

Upvotes: 1

Views: 572

Answers (3)

William Pursell
William Pursell

Reputation: 212208

for i in 1 2 3 4 5 6 7 8; do 
  for file in *; do 
    name="${file%%.*}"
    python script.py --parameter "$file" --out ../../output/directory"${i}/${cts_name}" \
        --readin "../../output/directory${i}/database_file_${i}.txt" 
  done
done

Upvotes: 2

wchmb
wchmb

Reputation: 1355

On a first attempt you can try this (but continue reading):

for f in $(ls -R); do
  if [[ $f =~ database_file_+([0-9])\.txt ]]; then
    python script.py ... --readin "$f"
  fi
done

Where +([0-9]) matches one or more numbers

Thanks to Charles Duffy comment, this snippet is much more accurate:

find . -type f -regex ".*/database_file_[0-9]+\.txt$" -exec python script.py ... --readin {} \;

Upvotes: 0

paulsm4
paulsm4

Reputation: 121609

It sounds like the key variable here is "#", not "file".

SUGGESTION:

for i in 1 2 3 4 5 6 7 8; do 
  filename=file$i.txt
  outdir=../../output/directory$i
  python script.py --parameter $filename --out $outdir/${cts_name} --readin $outdir/database_file_$i.txt 
done

PS: This assumes there's a 1::1 relationship between input file and #.

Otherwise, if there are N files for every #, you'll need a nested loop.

In any case, keep it as SIMPLE as possible (KISS).

Upvotes: 0

Related Questions