Executing 'grep' and 'who' with pipe in Java
843836Aug 29 2003 — edited Sep 3 2003Hi!
I am trying to execute following command using a Java program.
grep username | who | wc -l
I have written following code.
String command = "/bin/bash -c grep username | who | wc -l";
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p1.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p1.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s= stdInput.readLine()) != null)
{
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s= stdError.readLine()) != null)
{
System.out.println(s);
}
//System.exit(0);
I am getting following message at my prompt:
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
Also if I want to run
$who > temp
in java, can anyone please tell me how to run it?
thanks
bandya