I’m executing some commands from the command line in my java program, and it appears that it doesn’t allow me to use “grep”? I’ve tested this by removing the “grep” portion and the command runs just fine!
My code that DOESN’T work:
String serviceL = "someService";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("chkconfig --list | grep " + serviceL);
Code that does work:
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("chkconfig --list");
Why is this? And is there some sort of correct method or workaround? I’m aware that I could just parse the entire output, but I would find it easier to do it all from the command line. Thanks.
You’re trying to use piping which is a function of the shell … and you’re not using a shell; you’re exec’ing the
chkconfigprocess directly.The easy solution would be to exec the shell and have it do everything:
That being said … why are you piping to grep? Just read the output of
chkconfigand do the matching yourself in java.