1

This is my hello.sh shell 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 OUTPUT

Name: //this is wat it asks for

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

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

I've corrected my java code above..

Jan Hrcek
  • 626
  • 11
  • 24
hari
  • 1,297
  • 5
  • 17
  • 36
  • If ExpectJ is like vanilla Expect, you have to send a carriage return: `s.send("abcd\r");` – glenn jackman Apr 18 '11 at 13:52
  • @glenn: i don't have much idea about expect either. This is the first time i'm working on expectj. Anyways, i considered your suggestion,but unfortunately that didn't work.. – hari Apr 19 '11 at 05:20

2 Answers2

3

In your Java code you look for

s.expect("VALID_NAME=");

yet in your Bash code, you have:

echo "Name: "

It seems like simply changing your Java code to the following should work:

s.expect("Name: ");
AVH
  • 11,349
  • 4
  • 34
  • 43
  • @hari Seems to be like you need to use ``getCurrentStandardOutContents()``. And quite possibly also ``expectClose()``. See: http://expectj.sourceforge.net/apidocs/index.html. – AVH Apr 18 '11 at 12:41
  • @darhuuk: I've made the changes as u suggested..But no change in the output. – hari Apr 19 '11 at 05:06
  • @hari See DaveHowes comment in the other answer: you're asking ExpectJ for a String, but then don't do anything with it. If you want to show the String, print it: ``System.out.println(s.getCurrentStandardOutContents());``. – AVH Apr 19 '11 at 07:57
  • @hari Please upvote and accept my answer in that case. (And I'm sure @DaveHowes will appreciate it as well.) – AVH Apr 19 '11 at 10:05
0

I've never used ExpectJ, but the start of your error messagesh: hello.sh: No such file or directory suggests that it is unable to find your shell script. Have you tried it with the full path to the script?

String command = "sh /root/Desktop/hello.sh";
DaveH
  • 7,187
  • 5
  • 32
  • 53
  • @darhuuk: thanks..i've made the changes u guys suggested and it worked..but only to an extent..the new problem is its taking the input..but not displaying the corresponding output..M i missing something very silly here?? – hari Apr 18 '11 at 12:27
  • A \n here and there, perhaps? – Ingo Apr 18 '11 at 13:08
  • @Ingo: \n is not the problem here..I think I'm missing some statement..Not sure what that is.. – hari Apr 19 '11 at 04:30
  • s.getCurrentStandardOutContents() returns the contents of standard out, but you're not doing anything with it. What happens if you try System.out.println(s.getCurrentStandardOutContents()); ? – DaveH Apr 19 '11 at 07:19