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 |
run |
Returns a |
lines |
Returns immediately, while running the process in the background. The output that’s generated is provided through a Example:
|
lines_! |
Like the |
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 #> cmd2 |
Normally used to write to STDOUT but can be used like Example: |
cmd1 #&& cmd2 |
Run |
cmd1 #|| cmd2 |
Run |
cmd1 #&& cmd2#|| cmd3 |
Run |
The primary online documentation for the Scala process API is at these URLs:
- The scala.sys.process package object
- The
ProcessBuilder
trait