Shell Scripting with a Distributed Twist: Using the Sleep Scripting Language

Learn a Perl-like language whose scripts move around your network.

No one who isn't lazy writes scripts. Scripts save valuable system administrator time. In this article, I introduce the Sleep scripting language, which is a Perl-inspired language built on the Java platform. Although Java is sometimes a bad word in our community, Sleep can help you, because a Java-based language has several benefits. Scripts work on different platforms, data has the same form everywhere, and tools to solve any problem are available through the Java class library or open-source extensions.

With Sleep, you can save time on task automation and distributed computing. Sleep can help, whether you have one box or 10,000. Here, I introduce the language and its syntax, accessing the filesystem, talking to local and remote processes, and distributed computing with mobile agents.

Getting Started

You can use Sleep right away if you already have Java installed. Make sure the Java you use is the Sun Java. Any version 1.4.2 or later will do. Sleep does not run with the GNU Java that some Linux distributions use by default:

$ java -version
java version "1.5.0_13"
Java(TM) 2 Runtime Environment, Standard Edition

Installation is easy. Visit the home page (see Resources), and download the sleep.jar file. This file has everything you need to execute Sleep scripts:

$ wget

You can execute a script on the command line with the following:

$ cat >
println("I am $__SCRIPT__ with " . @ARGV);
$ java -jar sleep.jar "hello icecream" 34
I am with @('hello icecream', '34')

Sleep scripts also are happy to exist as UNIX script files:

#!/path/to/java -jar /path/to/sleep.jar
println("Hello Icecream!");

$ chmod +x script
$ ./script
Hello Icecream!

Sleep Basics

Sleep and Perl have a lot in common. Variables are scalars, and scalars store strings, numbers, functions or Java objects:

# Set some variables
$w = "foo";
$x = 3.14 * 12;
$y = &someFunction;
$z = [java.awt.Color RED];

Like Perl, Sleep comments begin with a # and end with a newline.

Variable names inside double-quoted strings are replaced with their value at runtime. For example, "this is a $x" will use the current value of $x. To avoid this behavior, prefix a variable with a backslash. Double-quoted strings can format variables to a small degree. Use "$[20]x" to pad the value of $x with spaces until it is 20 characters wide. A negative number prefixes the value with spaces. The $+ operator brings together the left and right values in a string. For example, "a $+ b" is "ab".

Like Perl, Sleep has arrays and hashes. An array refers to values by a numerical index:

@a = @("a", "b");
@a[2] = "c";
push(@a, "d");


@('a', 'b', 'c', 'd')

Hashes store and get values with a string key. Think of these as a dictionary. The keys are not kept in order:

%b = %(a => "apple", b => "bat");
%b["c"] = 'cat';


%(a => 'apple', c => 'cat', b => 'bat')

Scripts can create hashes of hashes, arrays of hashes, arrays of arrays, and any other combination you can imagine. These data structures offer a flexible way for storing data. And, these structures are more than hashes and arrays. Scripts can use arrays as sets, stacks, queues and lists. Combinations of arrays and hashes can make finite-state machines, graphs and trees. You can make nearly any data structure you'll need.

Sleep provides a gamut of flow control options. The for loop, while loop and foreach loop are all here. If statements work as you would expect. Sleep differentiates strings and numbers for comparisons. Here, I use the Sleep console to show the difference:

$ java -jar sleep.jar
>> Welcome to the Sleep scripting language
> ? "3" eq 3.0
> ? "3" == 3.0

The assignment loop is found a lot in Sleep scripts. This loop evaluates a statement and assigns the result to a variable before executing the loop body. The loop keeps going while the result is not $null, which is the empty value—it is equal to an empty string, the number zero and a NULL reference all at once. Most functions return $null when they are finished. This script iterates over each line of a file:

$handle = openf("/etc/passwd");

while $entry (readln($handle))

Sleep uses the same functions to work on files, processes and sockets. A scalar that holds a file, process or socket is a handle. The &readln function reads a line of text from a handle. The &println function prints a line of text. Likewise, &readb reads some bytes from a handle. And, &writeb writes bytes. The following is a Sleep version of the UNIX copy command:

global('$source $dest $handle');

($source, $dest) = @ARGV;

$handle = openf($source);
$data = readb($handle, -1);

$handle = openf("> $+ $dest");
writeb($handle, $data);

$ java -jar sleep.jar a.txt b.txt

Notice the value @ARGV. This array holds the script's command-line arguments. The &closef function closes a handle.

Scripts declare named functions with the sub keyword. Arguments are available as $1 to $n:

sub foo
   println("$1 and $2");

foo("bar", "baz");

bar and baz

Sleep functions are first-class types. This means you can assign them to variables and pass them as arguments to functions. A script can refer to a named function with &functionName. Scripts also can use anonymous functions—anonymous functions? Yes. An anonymous function is a block of code enclosed in curly braces:

$var = { println("hi $1"); };

# call the function in $var
[$var: "mom"];

# call an anonymous function
[{ println("hi $1"); }: "dad"];

hi mom
hi dad

Sleep invokes functions and talks to Java through object expressions. An object expression encloses an object, an optional message and arguments in square brackets:

[$object message: arg1, arg2, ...];

The example below shows nested object expressions:

[[System out] println: "Hello World"];


Free Dummies Books
Continuous Engineering


  • What continuous engineering is
  • How to continuously improve complex product designs
  • How to anticipate and respond to markets and clients
  • How to get the most out of your engineering resources

Get your free book now

Sponsored by IBM

Free Dummies Books
Service Virtualization

Learn to:

  • Define service virtualization
  • Select the most beneficial services to virtualize
  • Improve your traditional approach to testing
  • Deliver higher-quality software faster

Get your free book now

Sponsored by IBM