external command

How to run external shell commands in SBT (Simple Build Tool)

To run external shell commands in SBT, first start SBT from your operating system command line:

$ sbt

Then run the consoleProject task/command:

> consoleProject

After some output you’ll see this prompt:

scala>

Now you can execute shell commands by including them in double quotes, and following them by an exclamation mark, like this:

scala> "ls -al" !

For more information, see the SBT consoleProject documentation page.

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

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.20, “An index of 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.

Methods to execute external commands

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

Table 12-1. Methods to execute system commands

Scala: How to set environment variables when running external commands

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 12.19, “How to set environment variables when running external commands.”

Problem

You need to set one or more environment variables when running an external command.

Solution

Specify the environment variables when calling a Process factory method (an apply method in the Process object).

Scala: How to run an external command (process) in a different directory

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 12.18, “How to run an external command (process) in a different directory.”

Problem

You want to use another directory as the base directory when running an external command.

Scala: How to handle wildcard characters when running external commands

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.17, “How to handle wildcard characters when running external commands.”

Problem

You want to use a Unix shell wildcard character, such as *, in an external command.

How to use 'and' and 'or' when running external commands in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.16, “How to use 'and' and 'or' when running external commands in Scala.”

Problem

You want to use the equivalent of the Unix && and || commands to perform an if/then/else operation when executing external commands.

Solution

Use the Scala operators #&& and #||, which mirror the Unix && and || operators:

How to redirect the STDOUT and STDIN of external commands

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.15, “How to redirect the STDOUT and STDIN of external commands.”

Problem

You want to redirect the standard output (STDOUT) and standard input (STDIN) when running external commands. For instance, you may want to redirect STDOUT to log the output of an external command to a file.

Solution

Use #> to redirect STDOUT, and #< to redirect STDIN.

How to build a pipeline of external commands in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.14, “How to build a pipeline of external commands in Scala.”

Problem

You want to execute a series of external commands, redirecting the output from one command to the input of another command, i.e., you want to pipe the commands together.

Scala: How to handle the STDOUT and STDERR of external commands

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.13, “How to handle the STDOUT and STDERR of external commands.”

Problem

You want to run an external command and get access to both its STDOUT and STDERR.

Solution

The simplest way to do this is to run your commands as shown in previous recipes, and then capture the output with a ProcessLogger. This Scala shell script demonstrates the approach:

How to execute external commands and use their STDOUT in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.12, “How to execute external commands and use their STDOUT in Scala.”

Back to top

Problem

You want to run an external command and then use the standard output (STDOUT) from that process in your Scala program.

Back to top

Solution

Use the !! method to execute the command and get the standard output from the resulting process as a String.

Table of Contents

  1. Problem
  2. Solution
Back to top