Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

running shell script on Java

 
Tafta Zani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I'm trying to make a program that can gather information about hardware spec in a computer with Linux platform. I have a shell script that can gather that sort of information but i have a problem running it in Java.

My script contains piping (|) and redirection (> symbols and when i tried it in java it didn't work.

If i run

Process cmd_su = Runtime.getRuntime().exec("cat /proc/cpuinfo | grep proc");

BufferedReader ls_in = new BufferedReader(new InputStreamReader(cmd_su.getInputStream()));
try {
while ((ls_str = ls_in.readLine()) != null) {
System.out.println(ls_str);
}
}
catch (IOException e){
System.exit(0);
}

it didn't show anything in the screen

How do i make piping and redirection work inside Java?
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tafta,

After you executed your code did it return any kind of error message, if so it could help others help you if you also mention your error in the next post. On the other hand there might be no "proc" in your file, which I am sure is not the case.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piping and redirection isn't working with Java.

A detailed diskussion is found here: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

You may capture the outputstream of cmd_su, and use it as Inputstream for a second process (grep).

Maybe reading the file and search it by java would be more easy in this case.
 
Tafta Zani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the information.
Java IO is still too complicated for me so i'll go try build my own parser.
Could you tell me a java class suitable for making a text parser?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic