這是我的腳本ExpectJ冒險
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();
}
}
}
=================================== =========================================
這是我OUTPUT
Name:
Current status: Name:
correct username
Password:
================================== ==========================================
它沒有進一步發展。其並無終止或者..我不知道爲什麼..
是該行的作品,因爲它的印刷輸出。我猜這是沒有問題的..因爲控制是來s.expect(「密碼:」); ,但它沒有成功地從腳本中獲取pwd ..我不明白爲什麼會發生這種情況。 – hari 2011-04-20 12:46:02
控制權不是來自s.send(「pwd \ n」); – hari 2011-04-21 05:05:12