Home > Enterprise >  How to access sequential files using bash script and produce associated outputs?
How to access sequential files using bash script and produce associated outputs?

Time:10-28

I am a beginner in bash script, and I want to understand how to access files sequentially. I have files in the following format:

op_0.pdb
op_1.pdb
op_2.pdb
.
.
.
op_10000.pdb

I want to sequentially access these files using a loop and produce associated outputs in the same sequential manner. I have tried a bit using the following few lines but was unable to get the desired result.

#!/bin/bash
for i in {0..10000}
do
crysol_30 op_$i.pdb
done

Any help is appreciated.

For e.g the outputs files which I am getting are in following format:(these are outputs from first two files op_0.pdb and op_1.pdb)

op_000-water.pdb

op_000.log

op_000.int

op_000.alm

op_000.abs

op_100-water.pdb

op_100.log

op_100.int

op_100.alm

op_100.abs

I want output as op_0-water.pdb, op_0.log ...so on and so forth.

CodePudding user response:

You can craft the correct file name right in the beginning.

for pdb in op_{0..10000}.pdb; do
  crysol_30 "$pdb"
done

Or all files at once:

crysol_30 op_{0..10000}.pdb

But keep in mind, that the number of arguments is limited.

CodePudding user response:

With ten thousand files you're better off using a C style for loop:

#!/bin/bash

for (( i = 0; i <= 10000; i   )); do
     crysol_30 "op_$i.pdb"
done

This avoids the brace expansion's generation of a huge number of words to iterate through by using a simple counter instead.

  • Related