-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How o implement the pipe "|" #12
Comments
I solved the problem with the stupid solution as below: ProcResult result = new ProcBuilder("adb")
.withArgs("devices")
.run();
result = new ProcBuilder("tail")
.withArgs("-n", "+2")
.withInputStream(new ByteArrayInputStream(result.getOutputString().getBytes()))
.run();
if (!result.getOutputString().trim().isEmpty()) {
result = new ProcBuilder("awk")
.withArgs("{print $1}")
.withInputStream(new ByteArrayInputStream(result.getOutputString().getBytes()))
.run();
} Hope can hear the better solution. |
@ansonliao Thanks for this interesting experience report. JProc is currently geared towards being called synchronously, that is one process after another. This means the last solution, while not very efficient is the most idiomatic one given the current API. Here a couple of comments how you could make your solution a bit more elegant: You could use ProcResult result = new ProcBuilder("adb")
.withArgs("devices")
.run();
result = new ProcBuilder("tail")
.withArgs("-n", "+2")
.withInput(result.getOutputString())
.run();
if (!result.getOutputString().trim().isEmpty()) {
result = new ProcBuilder("awk")
.withArgs("{print $1}")
.withInput(result.getOutputString())
.run(); Further you could use the provided static helper methods: String result = ProcBuilder.filter(
ProcBuilder.filter(
ProcBuilder.run("adb", "devices"),
"tail", "-n", "+2"),
"awk", "{print $1}"
); For your particular use case, I would actually recommend performing the second and the third step in Java. Like so: String[] lines = ProcBuilder.run("adb", "devices")
.split("\n");
List<String> result = asList(lines)
.subList(Math.max(0, lines.length - 3), Math.max(0, lines.length - 1))
.stream().map(line -> line.split("\\s+")[0]).collect(Collectors.toList()); The java solution could also be implemented in a streaming fashion using |
Proper pipelining is currently a hassle to implement with JProc. In theory a pair of So I'll outline a potential solution here. It would be much nicer to have a declarative interface that takes a couple of process builders and takes care of the spawning connecting and monitoring the processes. To stick with the example given here, something like this: PipelineResult result = new PipelineBuilder(
new ProcBuilder("adb").withArgs("devices"),
new ProcBuilder("tail").withArgs("-n", "+2"),
new ProcBuilder("awk").withArgs("{print $1}")
)
.withTimeout(10000)
.run();
result.getOutputString() The |
@fleipold thanks for your reply and looking for the |
Hi @fleipold , my ProcResult result = new ProcBuilder("adb")
.withArgs("devices")
.run();
result = new ProcBuilder("tail")
.withArgs("-n", "+2")
.withInput(result.getOutputString())
.run(); I can't find method |
@ansonliao good catch. The |
Hi,
I would like to execute some commands with
JProc
library and meet some problem, after studied the tutorial still can't get the solution, so ask some help here.My command is simple with some pipes, for example,
the simple command is:
# adb devices List of devices attached emulator-5554 device
and then I would like to add one pipe:
#adb devices | tail -n +1 emulator-5554 device
and then finally I would like to add more one pipe:
#adb devices | tail -n +2 | awk '{print $1}' emulator-5554
so my script is:
it doesn't work.
and also
it also doesn't work.
Could you help to provide some solution for my question?
Thanks.
The text was updated successfully, but these errors were encountered: