Running multiple unix command using Java ProcessBuildr

时间:2015-07-28 15:42:01

标签: java unix process

I am running following unix command using ProcessBuilder and it is working fine.

String[] commands = {"egrep","search string","fileName"} ProcessBuilder pb =...

Now I have an additional requirement to filter the output.

String [] commands = {"egrep","search string","fileName","|","awk\'$0 >\"time\" && $2==\"INFO\"\'"}

I am getting error "IO Exception: No such file error". It seems it is considering pipe(|) and other awk command as file.

I also tried adding prefix "bash -c" or "/bin/sh -c" like

String [] commands = {"bash","-c","egrep","search string","fileName","|","awk\'$0 >\"time\" && $2==\"INFO\"\'"}

Now I am getting error "bash -c line0: syntax error near unexpected token 'is'bash:-c line0:"

I also tried giving entire egrep command in single string but it also didn't work.

Please advice what am I missing error to use pipe for filtering the output.

1 个答案:

答案 0 :(得分:0)

不考虑您提出的egrep / awk命令行是否有效 - 我不确定它是什么意思 - 考虑将完整命令行作为{{1}的-c选项的单个参数因此,您在构建ProcessBuilder时只涉及三个字符串。有关详细信息,请参阅SO question 3776195