0

これは私のスクリプトです

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:

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

それ以上進んでいない.どちらも終了していない..理由がわからない..

4

2 に答える 2

1

次の行にコメントすると機能しますか:

System.out.println("Current status: "+s.getCurrentStandardOutContents());

おそらく、アプリケーションは値を「期待」しています"correct username"が、"Current status: Name:代わりに「」を参照しています (「デバッグ」行からの出力)。jExpert の専門家ではありませんが、ツールが単にリダイレクトして監視するだけの場合、System.outスクリプト出力とすべてが表示されます。コンソールに出力します。

于 2011-04-20T09:57:48.847 に答える
0

私はそれを取得します..2つの連続したs.expect()ステートメントは決して機能しません..それを取り除くために、\nを追加できます..

この場合

s.expect("正しいユーザー名");
s.expect("パスワード:");

は機能しないはずです。したがって、次のものに置き換える必要があります-

s.expect("正しいユーザー名\nパスワード:");//これでうまくいきます

于 2011-05-11T08:37:53.643 に答える