Scala: An index of methods to run/execute external system commands

This is an excerpt from the Scala Cookbook. This is Recipe 12.20, “An index of Scala methods available to run external system commands.”

The following tables list the methods of the scala.sys.process package that you can use when running external (system) commands.

Scala methods to execute external commands

Table 12-1 lists the Scala methods that you can use to execute external system commands.

Table 12-1. Methods to execute system commands

Method Description
!

Runs the command and returns its exit code. Blocks until all external commands exit. If used in a chain, returns the exit code of the last command in the chain.

!!

Runs the command (or command pipe/chain), and returns the output from the command as a String. Blocks until all external commands exit. Warning: throws exceptions when the command’s exit status is nonzero.

run

Returns a Process object immediately while running the process in the background. The Process can’t currently be polled to see if it has completed.

lines

Returns immediately, while running the process in the background. The output that’s generated is provided through a Stream[String]. Getting the next element of the Stream may block until it becomes available. Throws an exception if the return code is not zero; if this isn’t desired, use the lines_! method.

Example:

    scala> val x = Process("ls").lines
     x: Stream[String] = Stream(Bar.scala, ?)

lines_!

Like the lines method, but STDERR output is sent to the ProcessLogger you provide. Per the Scaladoc, “If the process exits with a nonzero value, the Stream will provide all lines up to termination but will not throw an exception.” Demonstrated in Recipe 12.12.

Scala methods to redirect STDIN and STDOUT

Table 12-2 lists the methods that you can use to redirect STDIN and STDOUT when external commands are executed.

Table 12-2. Methods to redirect STDIN and STDOUT

Method Description
#<

Read from STDIN

#>

Write to STDOUT

#>>

Append to STDOUT

Scala methods to combine external commands

Table 12-3 lists the methods that you can use to combine (pipe) external commands.

Table 12-3. Methods to combine external commands

Name Description
cmd1 #| cmd2

The output of the first command is used as input to the second command, like a Unix shell pipe.

cmd1 ### cmd2

cmd1 and cmd2 will be executed in sequence, one after the other. This is like the Unix ; operator, but ; is a reserved keyword in Scala.

cmd1 #> cmd2

Normally used to write to STDOUT but can be used like #| to chain commands together.

Example:

    scala> ("ps aux" #> "grep java" #> "wc -l").!!.trim
     res0: String = 2

cmd1 #&& cmd2

Run cmd2 if cmd1 runs successfully (i.e., it has an exit status of 0).

cmd1 #|| cmd2

Run cmd2 if cmd1 has an unsuccessful (nonzero) exit status.

cmd1 #&& cmd2#|| cmd3

Run cmd2 is cmd1 has a successful exit status, otherwise, run cmd3.

The primary online documentation for the Scala process API is at these URLs:

  • The scala.sys.process package object
  • The ProcessBuilder trait