如何自动运行提示用户输入的命令?

时间:2013-04-10 04:33:27

标签: automation expect

我有一个要求输入的程序。例如:

$ program arg1 arg2
Enter the value of arg3: foo
Enter the value of arg4: spam
$

如何自动运行?我怀疑expect提供了相应的功能,但以下内容对我不起作用:

#!/usr/bin/expect
set timeout 20
spawn "./program arg1 arg2"
expect "Enter the value of arg3:" { send "foo\r" }
expect "Enter the value of arg4:" { send "spam\r" }
interact

有没有人有想法?感谢。

2 个答案:

答案 0 :(得分:1)

尽可能以最简单的方式尝试(无需预期):这有用吗?

program arg1 arg2 <<END
foo
spam
END

(或printf "%s\n" foo spam | program arg1 arg2

答案 1 :(得分:0)

spawn "./program arg1 arg2"应为spawn ./program arg1 arg2