需要 Expect/ExpectJ 专家的帮助

发布于 2024-11-04 06:13:54 字数 880 浏览 2 评论 0原文

可以说我有以下 shellscript 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.

知道为什么会发生这种情况吗?或者它应该只是这样表现吗?

Lets say i have the following shellscript details.sh

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

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

And this is my Java code

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.

Any idea why that's happening?Or is it supposed to behave that way only?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生面具三千个 2024-11-11 06:13:54

看看你的脚本输出,我会输入以下期望

s.expect("hi\nhello\nbye");

Looking at your script output, I would put in the following expect

s.expect("hi\nhello\nbye");
九厘米的零° 2024-11-11 06:13:54

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

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

s.expect("hi");

我得到的输出将是-


你好
再见

所以没有什么可担心的。

Ok it would display the same strings even though u only one of them. It displays all those strings which are consecutive. I've learnt that its the natural behavior of expectj.Spawn.expect(). That's how it works.

In this case, even if i put-

s.expect("hi");

the output i'll get will be-

hi
hello
bye

So there is nothing to be worried about.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文