bfd*_*bfd 5 command-line scripting bash shell-script
我需要编写一个 shell 脚本来通过现有程序自动运行一组文件 ~1000 次。我试图运行它们的程序是通过命令行访问的,如下所示,然后通过键入“加载文件类型文件名”来加载您希望程序使用的文件,如下所示:
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
Run Code Online (Sandbox Code Playgroud)
(...这里有很多输出)
每次运行都会更改的文件是phe_dbpsim2e1.txt
,其名称每次运行都会增加一个数字。我能够从脚本执行程序,但是一旦程序打开,它就无法识别我尝试通过脚本输入的任何命令(即加载),并且程序等待我手动输入命令. 退出程序后,脚本中的所有命令都会显示在屏幕上,因此我编写的脚本似乎在程序打开时暂停。
有没有办法在程序打开后将命令输入到程序中而无需手动输入?
如果fbat
坚持从终端获取其输入并且您想自动化,则解决方案是使用expect
(或pexpect
)。这是一个expect
脚本示例,它可能会自动执行您的程序:
#!/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"
Run Code Online (Sandbox Code Playgroud)
因为我没有访问权限fbat
,所以上面当然没有经过测试。
要expect
在类似 debian 的系统上安装,请运行:
apt-get install expect
Run Code Online (Sandbox Code Playgroud)