MUD
MUD

Reputation: 121

Nested for loops to run input files using bash script

I have a series of input files I want to run using a bash script. These are identified using a file location (the same for all files) and an individual file suffix made up of the name of the type of input and the temperature. I want my script to loop over name, then temperature, in order to run the files in this order:

file_location_a_300

file_location_a_290

file_location_a_281

file_location_a_270

file_location_b_300...

And so on and so forth.

This is what I have so far. My issue at present is that the $i name caller in INPUT and OUTPUT isn't working. I get the error "Failed to open input file "file_location_300". The echo line works fine and outputs both the name and the temperature. I don't understand why the name is being omitted in the input and output file caller line?

#!/bin/bash

temps=(300 290 281 270)
names=(a b c)

for i in "${names[@]}";

    do

    for j in "${temps[@]}";

        do
        [FILE EXECUTABLE INFORMATION] 
        INPUT=file_location_$i_$j OUTPUT=../out/file_location_$i_$j.out  
        echo $i $j

    done

done

Upvotes: 0

Views: 228

Answers (2)

glenn jackman
glenn jackman

Reputation: 246807

You can use bash brace expansion:

for input in file_location_{a,b,c}_{300,290,281,270}; do
    output="../out/$input.out"
    ...
done

Get out of the habit of using ALLCAPS variable names, leave those as reserved by the shell. One day you'll write PATH=something and then wonder why your script is broken.

Upvotes: 1

Christian Fritz
Christian Fritz

Reputation: 21364

That's because bash thinks you are referring to a variable called i_.

This works:

INPUT=file_location_${i}_${j} OUTPUT=../out/file_location_${i}_${j}.out  

Upvotes: 2

Related Questions