LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-22-2006, 08:51 PM   #1
BlueRaja
LQ Newbie
 
Registered: Jul 2004
Posts: 8

Rep: Reputation: 0
Scripting with pipes and Java


I have a directory of folders, each containing a number of .class (Java) files. I would like to write a script to run each one and test it on a number of given inputs.
In otherwords, when I normally type "java myClass" in one of the folders, a console menu will appear and ask for some input. I might choose option "1" and hit enter; then it will ask me, say, what my name is - I'll type in my name, and it will bring the menu back up; and so on and so forth.
I can probably figure out how to traverse all the folders (I just began scripting today), but I can't seem to figure out how to run the test cases on the java files. I tried scripts as simple as these:
Code:
echo -e "1\nBlueRaja" | java myClass
Code:
(echo 1; echo BlueRaja) | java myClass
and more complex code (using a named pipe) like this:
Code:
#!/bin/csh
mkfifo pipe
java myClass <pipe >output.out &
echo -e "a\n" > pipe
echo -e "BlueRaja\n" > pipe

exit 0
However, nothing seems to work. If anyone could lend me some of their insight, it would be greatly appreciated.

Thank you.
 
Old 09-23-2006, 02:02 AM   #2
BlueRaja
LQ Newbie
 
Registered: Jul 2004
Posts: 8

Original Poster
Rep: Reputation: 0
The Java program reads input using the following code, if this helps:
Code:
InputStreamReader inStream = new InputStreamReader(System.in);
BufferedReader    stdin    = new BufferedReader(inStream);
String            input    = stdin.readLine();
 
Old 09-23-2006, 03:15 AM   #3
elsheikhmh
Member
 
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
hi,
try test.sh
Code:
java myClass <<DELIMITER
7
DELIMITER
replace 7 by whatever you want to enter if the program were running interactively.

bye,
mustafa
 
Old 09-23-2006, 11:50 AM   #4
BlueRaja
LQ Newbie
 
Registered: Jul 2004
Posts: 8

Original Poster
Rep: Reputation: 0
both of these work:
Code:
echo 1 | java myClass
Code:
java myClass <<DELIMITER
1
DELIMITER
However, I would like to input more than one thing after another using a script - the following does not work:
Code:
java myClass <<DELIMITER
1
BlueRaja
DELIMITER
 
Old 09-23-2006, 02:16 PM   #5
BlueRaja
LQ Newbie
 
Registered: Jul 2004
Posts: 8

Original Poster
Rep: Reputation: 0
Also: According to everything I've read, this should work:
Code:
input.txt:
1
BlueRaja
Code:
user%   java myClass < input.txt
This works in Windows, but not Linux (in two separate Linux machines) - I've tried everything I can think of, and am just about to give up with Linux and just do it in a batch file...
 
Old 09-24-2006, 05:12 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,483

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
The problem must be in your java code, I believe the first bufferedReader is flushed somewhere there.

A simple Java program using your sample code works fine under Unix:
Code:
import java.io.*;

public class Test
{
  public static void main(String args[]) throws Exception
  {
    try
    {
      InputStreamReader inStream = new InputStreamReader(System.in);
      BufferedReader    stdin    = new BufferedReader(inStream);
      String            input    = stdin.readLine();
      System.out.println("input="+input);
      input    = stdin.readLine();
      System.out.println("input="+input);
    }
    catch(Exception e)
    {
      throw(e);
    }
  }
}

- = - = - = - = -

$ java Test <<%EOF%
one
two
%EOF%
input=one
input=two
$
 
Old 09-26-2006, 03:52 AM   #7
BlueRaja
LQ Newbie
 
Registered: Jul 2004
Posts: 8

Original Poster
Rep: Reputation: 0
Wait...I'm *not* supposed to flush the BufferedReader? o_O
Wow thanks, I think I can get it working now... but why does flushing BufferedReader screw it up?
 
Old 09-26-2006, 05:53 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,483

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Because you discard the remaining lines before giving a chance your application to read them.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripting with pipes and Java BlueRaja Programming 1 09-24-2006 05:16 AM
pipes smart girl Programming 6 04-30-2006 10:20 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
about pipes kpachopoulos Programming 1 10-15-2005 12:37 PM
Shell scripting from java file karthiknataraj Linux - General 1 02-08-2003 04:47 AM


All times are GMT -5. The time now is 09:42 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration