shell

How to start a Scala REPL session inside SBT (Simple Build Tool)

Scala FAQ: How do I start a Scala REPL session from SBT (i.e., inside the Simple Build Tool)?

Answer: Use console or consoleQuick:

  • Type console to start a REPL session from inside SBT. This (a) compiles your Scala project and then (b) starts a REPL session.
  • Type consoleQuick if you don’t want to compile your project before starting a REPL session inside of SBT.

The console command process looks like this:

> sbt
[info] Loading project definition from /Users/al/Projects/Cats101/project
[info] Set current project to Cats101 (in build file:/Users/al/Projects/Cats101/)

> console
[info] Starting scala interpreter...
[info]
Welcome to Scala 2.12.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_131).
Type in expressions for evaluation. Or try :help.

scala> _

If you wanted to see how to start a Scala REPL session from inside SBT, I hope this helps.

How to use ScalaCheck in the SBT console

If you add ScalaCheck to an SBT project like this:

libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.13.4" % "test"

it’s only available in the SBT “test” scope. This means that when you start a Scala REPL session inside of SBT with its console command, the ScalaCheck library won’t be available in that scope.

To use ScalaCheck with the SBT console (REPL), don’t use its console command — use test:console instead. A complete example looks like this:

$ sbt

> test:console

scala> import org.scalacheck.Gen.choose

Note that after you type test:console your project may be compiled, so that step may take a few moments.

In summary, use SBT’s console command to start a “normal” Scala REPL inside SBT, and use test:console to start a REPL that you can run tests inside of. (Note that this same advice also applies to using ScalaTest or specs2.)

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.

How to run shell commands from the Scala REPL

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.4, “How to run a shell command from the Scala REPL.”

Problem

You want to be able to run a shell command from within the Scala REPL, such as listing the files in the current directory.

Solution

Run the command using the :sh REPL command, then print the output. The following example shows how to run the Unix ls -al command from within the REPL, and then show the results of the command:

How to make your Scala shell scripts run faster by pre-compiling them

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 14.13, “How to make your Scala shell scripts run faster by pre-compiling them.”

Problem

You love using Scala as a scripting language, but you’d like to eliminate the lag time in starting up a script.

Solution

Use the -savecompiled argument of the Scala interpreter to save a compiled version of your script.

A basic Scala script like this:

How to prompt users for input from Scala shell scripts

This is an excerpt from the Scala Cookbook, partially modified for the internet. This is Recipe 14.12, “How to prompt users for input from Scala shell scripts.”

Back to top

Problem

You want to prompt a user for input from a Scala shell script and read her responses.

Back to top

Solution

Use the readLine, print, printf, and Console.read* methods to read user input, as demonstrated in the following script. Comments in the script describe each method:

Table of Contents

  1. Problem
  2. Solution
Back to top

How to use Scala as a scripting language

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.10, “How to use Scala as a scripting language.”

Back to top

Problem

You want to use Scala as a scripting language on Unix systems, replacing other scripts you’ve written in a Unix shell (Bourne Shell, Bash), Perl, PHP, Ruby, etc.

Back to top

Solution

Save your Scala code to a text file, making sure the first three lines of the script contain the lines shown, which will execute the script using the scala interpreter:

Table of Contents

  1. Problem
  2. Solution
Back to top

Getting started with the Scala REPL (command-line shell)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.1, “How to get started with the Scala REPL.”

Back to top

Problem

You want to get started using the Scala REPL, including understanding some of its basic features, such as tab completion, starting the REPL with different options, and dealing with errors.

Back to top

Solution

To start the Scala REPL, type scala at your operating system command line:

Table of Contents

  1. Problem
  2. Solution
Back to top

How to copy files to an Android emulator’s data directory with ‘adb push’

As an Android developer, you can normally use the adb push command to copy files from your computer’s hard drive to an Android device. However, I just ran into a problem where I couldn’t copy files to my Android emulator’s “data” directory, i.e., the /data/data filesystem. When I tried to copy a file using this command:

$ adb push foo.jpg /data/data/com.alvinalexander.myapp/files

I got this Android error:

Raspberry Pi camera module shell script

As a quick note today, this is the source code for a Raspberry Pi (RPI) camera module shell script that I created so a friend can turn her Raspberry Pi camera on and off from the RPI command line (Linux command line):