Search code examples
linuxshellterminalexecutablecsh

Run an Executable Terminal Program in Linux for Multiple Files to Produce Multiple Output Files


I have an executable program that is run in the Linux terminal. The program works as follows:

In terminal I enter the name of the program. Then it gives me the following prompts: outputfile, times, input file, option, ect. I'm trying to create a script to run the program on all files in the directory. All of the prompts that the program gives are the same, except the output and input files differ from file to file. There are approximately 300 input files named 001h.pdb ... 300h.pdb. I need the output files to be 001p.acc ... 300p.acc.

(Also, the responses to the program's prompts are: "outputfile", 1, "inputfile", bnl, next, next, allatm, next, next, no.)

What would be a reasonable csh script?


Solution

  • I think something like this might do the trick...

    #!/bin/bash
    
    # Loop through all .pdb files
    for f in *.pdb
    do
       echo DEBUG: Processing $f
       out=${f/h.pdb/p.acc}     # Determine name of output file
    
       # Echo all parameters needed by program "prog"
       ( echo "$out"
         echo 1
         echo "$f"
         echo bnl
         echo next
         echo next
         echo allatm
         echo next
         echo next
         echo no )  | ./prog
    done
    

    Save the above in a file called auto, then type the following to firstly change its mode (and thereby make it executable) and then run it:

    chmod +x auto
    ./auto
    

    Depending whether you value conciseness over readability, you may or may not prefer the following which is identical in function:

    #!/bin/bash
    
    # Loop through all .pdb files
    for f in *.pdb
    do
       out=${f/h.pdb/p.acc}     # Determine name of output file
    
       echo DEBUG: Processing $f into $out
    
       # Echo all parameters needed by program "prog". Note "\n" is a newline
       echo -e "$out\n1\n$f\nbnl\nnext\nnext\nallatm\nnext\nnext\nno" | ./prog
    done