0

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.

  • What is your desired output? What is the result currently you get? – tshiono Oct 27 '21 at 05:06
  • Your approach is not wrong: In each iteration, you invoke the program `crysol_30` with one of the filenames, one after the other. You get exactly 10000 invocations of `crysol_30`, which means that many child processes. You may consider for performance reason to modify `crysol_30` to loop over the files internally. Also consider the possibility that maybe one day there would be 9867 files or 10014. Perhaps it makes sense to make your script a bit more flexible in this direction. – user1934428 Oct 27 '21 at 07:06
  • `I have tried a bit using the following few lines but was unable to get the desired result` Do you know why? Your code looks perfectly fine. – KamilCuk Oct 27 '21 at 07:59

2 Answers2

0

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.

ceving
  • 21,900
  • 13
  • 104
  • 178
0

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.

Shawn
  • 47,241
  • 3
  • 26
  • 60