Bash 使用Expect实现交互式程序的部分自动化
我有一个程序的可执行文件,用Fortran编写,需要交互输入,例如20个输入。我想要的是,通过输入重定向提供前19个输入,但最后一个输入来自我的键盘。原因是当我运行程序时,我收到一条消息Bash 使用Expect实现交互式程序的部分自动化,bash,input,automation,expect,Bash,Input,Automation,Expect,我有一个程序的可执行文件,用Fortran编写,需要交互输入,例如20个输入。我想要的是,通过输入重定向提供前19个输入,但最后一个输入来自我的键盘。原因是当我运行程序时,我收到一条消息 file list_wall.dat written - modify it to group walls modify wallList.dat (add flag type) - enter 1 when ready 所以在我按1之前,我需要手动修改一个文件,然后手动按1。我尝试使用expect和以下代码
file list_wall.dat written - modify it to group walls
modify wallList.dat (add flag type) - enter 1 when ready
所以在我按1之前,我需要手动修改一个文件,然后手动按1。我尝试使用expect和以下代码自动执行此操作:
expect
spawn ./my_interactive_program
set fh [open input_file.in r]
while {[gets $fh line] != -1} {
send -- "$line\r"
}
close $fh
interact
当我从命令行手动运行此命令时,一切正常。但是,当我尝试将其添加到bash脚本文件并运行脚本时,它不起作用。这是我在bash脚本中编写的内容:
#!/bin/bash
expect <<'finished'
spawn ./my_interactive_program
set fh [open MBC4_Row1_Pitch1.in r]
while {[gets $fh line] != -1} {
send -- "$line\r"
}
close $fh
interact
finished
我能做些什么来解决这个问题?我做错什么了吗?
非常感谢。bash脚本中的输入重定向阻止提供交互式输入。
只需不使用bash,为您的工作代码编写一个expect脚本:
#!/usr/bin/expect -f
spawn ./my_interactive_program
set fh [open MBC4_Row1_Pitch1.in r]
while {[gets $fh line] != -1} {
send -- "$line\r"
}
close $fh
interact
在spawn命令之前添加
exp_internal 1
,查看expect的调试输出告诉您什么。另外,使用bash-xmyscript运行bash脚本
#!/usr/bin/expect -f
spawn ./my_interactive_program
set fh [open MBC4_Row1_Pitch1.in r]
while {[gets $fh line] != -1} {
send -- "$line\r"
}
close $fh
interact