How to run Unix script shell in Java code using ExpectJ tool?

This is my shell hello.sh script

VALID_NAME="abcd"

echo "Name: "

read name

if [ "$name" == $VALID_NAME ]; then

echo "correct"

else

echo "unexpected input"

fi

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

This is my java code

import java.io.IOException;

import expectj.*;

public class Trial {
public static void main(String[] args) {

    ExpectJ exp = new ExpectJ();
    String command = "sh /root/Desktop/hello.sh";
    Spawn s;
    try {
        s = exp.spawn(command);
        s.expect("Name: ");
        s.send("abcd");
        System.out.println(s.getCurrentStandardOutContents());
        s.stop();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TimeoutException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch (ExpectJException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}
}

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

And this is my way out

Name: // this is what it asks for

abcd // this is what I give .. and nothing happens

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

I adjusted my Java code above ..

+1
source share
2 answers

In Java code you are looking

s.expect("VALID_NAME=");

still in your Bash code you have:

echo "Name: "

It seems like just changing the Java code to the following:

s.expect("Name: ");
+3
source

ExpectJ, sh: hello.sh: No such file or directory , script. script?

String command = "sh /root/Desktop/hello.sh";
0

All Articles