Is it possible to nest command calls in the Scala Ammonite shell?

201 views Asked by At

I'm trying to translate a typical UNIX command jmap -heap $(pgrep java) to a command for the Ammonite-Shell.

My attempt so far: %'jmap("-heap", %'pgrep("java")).

I get the error SyntaxError: found "%'jmap(\"-heap\", %'pg", expected "while" | ... which seems to indicate the inner call to pgrep was not actually translated as a command call and just passed in as a string.

1

There are 1 answers

4
adamwy On BEST ANSWER

In Ammonite, single % spawns a subprocess and prints out its result. In order to access command output you need to use %% operator, which will return CommandResult object. This object contains out field which is a stream of its output.

You can nest it this way:

%jmap("-heap", (%%pgrep("java")).out.lines.take(1))