5

I need to write a shell script to automatically run a set of files through an existing program ~1000 times. The program I'm trying to run them through is accessed through command line as follows, then you load the files you want the program to use by typing in "load filetype filename" as follows:

server>./fbat

               *******************************************************
               *                                                     *
               *     *********  * * *          *       *********     *
               *     *          *     *       * *          *         *
               *     *******    *  * *       *   *         *         *
               *     *          *     *     * *** *        *         *
               *     *          *     *    *       *       *         *
               *     *          * * *     *         *      *         *
               *                                                     *
               *          Xin Xu  C1999-2009       v2.0.4Q       *
               *          Program for Population Genetics            *
               *          Harvard School of Public Health            *
               *                                                     *
               *******************************************************

>>load map leprmap.txt

read in 899 markers' info

>>load ped leprped.txt

read in: 899 markers from 16 pedigrees (338 nuclear families,1182 persons)

>>load phe phe_dbpsim2e1.txt

1 quantitative traits have been successfully read
719 persons have been phenotyped

>>trait resid

affection resid** 

>>fbat -v1 -e

(...lots of output here)

The file that will change with each run is phe_dbpsim2e1.txt, the name of which increments by one number each run. I'm able to execute the program from a script, but once the program is open, it doesn't recognize any of the commands I am trying to input through the script (i.e. load) and the program waits for me to manually input commands. Once I quit the program, all of my commands from the script show up on the screen, so it seems like the script I wrote is paused while the program is open.

Is there a way to input commands into the program once it's open without typing them in by hand?

  • Can you please try to make your question clearer? For example, what do you mean by, “once the program is open, it doesn't recognize any of the commands I am trying to input through the script (i.e. load)” and “all of my commands from the script show up on the screen”? What are you trying? What happens if you type echo "load map leprmap.txt" | ./fbat? Do you need to run fbat once, with 5000 commands sent to it? Or do you need to run it 1000 times, with five commands each time? You say “a set of files”, but you also say “the name … increments by one … each run”, which are not the same thing. – G-Man Oct 31 '14 at 21:43
  • Sorry for the lack of clarity - I meant that I have to run fbat 1000 times with 5 commands each. Thank you for your help! – bdarst Oct 31 '14 at 22:49
5

If fbat insists on getting its input from the terminal and you want to automate, the solution is to use expect (or pexpect). Here is an example of an expect script which might automate your program:

#!/usr/bin/expect -f
spawn ./fbat
expect ">>"
send "load map leprmap.txt\r"
expect ">>"
send "load phe phe_dbpsim2e1.txt\r"
expect ">>"
send "trait resid\r"

Because I don't have access to fbat, the above is, of course, not tested.

To install expect on a debian-like sytem, run:

apt-get install expect
  • 1
    Thank you so much @John1024!!! This seems to be exactly what I needed!! I really appreciate your help. – bdarst Oct 31 '14 at 22:28

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.