0

I have several input files to run a program, and I organized them in a way that the names of the input files are numbers. I would like to know if it is possible to write a loop to call these different input files, or in others words, to call a input file which name is a variable of the loop.

To be more specific, I created the input files in fortran, which names are "fort.XXX", where the XXX is a number, from 1 to 100. I call each of this inputs with a command like "program fort.XXX" Is there a way to create a loop in which I will write a command that is part constant ("program fort.") and part variable ("XXX")?

Many thanks in advance,

Cayo.

1

If you want to use the names of those existing files you can use something like this:

for FILE in fort.*
do
    echo "$FILE"
done

It will use all files in the current directory starting from fort. regardless of number of files. Of course, you can use any other command in place of echo, here.

| improve this answer | |
0

Here's an example that uses a range with the bash for loop:

#!/bin/bash
for i in {1..100}
do
echo "program fort.${i}"
done

Which should give you the following output:

program fort.1
program fort.2
program fort.3
program fort.4
...
program fort.99
program fort.100
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.