Expect / ExpectJ专家需要帮助

时间:2011-04-28 05:34:29

标签: java shell expect

假设我有以下的shellcript details.sh

echo "name"
read name
if [ "$name" == "abcd" ]; then
echo "hi"
echo "hello"
echo "bye"
fi

=============================================

这是我的Java代码

ExpectJ exp = new ExpectJ();
String cmd = "sh details.sh"; //Command for execution of **details.sh**
Spawn s = exp.spawn(cmd); //Spawns the process **details.sh**
s.expect("name");  //Expecting "name" from the script
s.send("abcd");  //When successful, it sends **abcd** as the input to the script

//Now the script will compare the input(given by java code) with the pre-fed one (the one in the script)

s.expect("hi");  //according to me only **hi** must be displayed,but the java console is taking and displaying all the consecutive echo statements from the script.

知道为什么会这样吗?或者它应该只是这样做?

2 个答案:

答案 0 :(得分:0)

查看脚本输出,除了

之外,我将添加以下内容
s.expect("hi\nhello\nbye");

答案 1 :(得分:0)

好吧它会显示相同的字符串,即使你只有其中一个。它显示所有连续的字符串。我已经了解到它是 expectj.Spawn.expect()的自然行为。这就是它的工作原理。

在这种情况下,即使我把 -

s.expect( “HI”);

我得到的输出将是 -

喜 你好 再见

所以没有什么可担心的。