这是我的脚本
echo "Name:"
read name
if [ "$name" == "abcd" ]; then
echo "correct username"
echo "Password:"
read password
if [ "$password" == "pwd" ]; then
echo "Hello"
else
echo "Wrong password"
fi
else
echo "wrong username"
fi=================================================================================
这是我的Java代码
import java.io.IOException;
import java.util.*;
import expectj.*;
public class Trial {
public static void main(String[] args) {
ExpectJ exp = new ExpectJ();
String command = "sh /root/Desktop/hello.sh";
Spawn s = null;
try {
s = exp.spawn(command);
s.expect("Name:");
s.send("abcd\n");
System.out.println("Current status: "+s.getCurrentStandardOutContents());
s.expect("correct username");
s.expect("Password:");
s.send("pwd\n");
s.expect("Hello");
System.out.println("final output: " + s.getCurrentStandardOutContents());
System.out.println("Possible errors: " + s.getCurrentStandardErrContents());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("ioe\n");
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("toe\n");
} finally {
if (s != null)
s.stop();
}
}
}============================================================================
这是我的输出
Name:
Current status: Name:
correct username
Password:============================================================================
它不进行further.Its也不终止..。我不知道为什么。
发布于 2011-05-11 08:37:53
我得到了.两个连续的s.expect()语句永远无法消除它,可以添加\n。
在这种情况下
S.expect(“正确用户名”);
S.expect(“密码:”);
与work.So无关,应改为-
S.expect(“正确的用户名\n nPassword:”);//这将起作用
发布于 2011-04-20 09:57:48
当您评论这一行时,它是否有效:
System.out.println("Current status: "+s.getCurrentStandardOutContents());也许应用程序是在“期待”值"correct username",但看到的是"Current status: Name:“(来自”调试“行的输出)。不是jExpert方面的专家,但是如果工具只是重定向和监视System.out,它将看到脚本输出以及您打印到控制台的所有内容。
https://stackoverflow.com/questions/5728318
复制相似问题