Bash – How to run a program that has its own command line within a shell script program

bashcommand linescriptingshell-script

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?

Best Answer

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
Related Question