Shell Scripting with a Distributed Twist: Using the Sleep Scripting Language
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.
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 http://sleep.dashnine.org/download/sleep.jar
You can execute a script on the command line with the following:
$ cat >tryit.sl
println("I am $__SCRIPT__ with " . @ARGV);
$ java -jar sleep.jar tryit.sl "hello icecream" 34
I am tryit.sl 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 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");
println(@a);
@('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'; println(%b); %(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 false > ? "3" == 3.0 true
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))
{
println($entry);
}
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);
closef($handle);
$handle = openf("> $+ $dest");
writeb($handle, $data);
closef($handle);
$ java -jar sleep.jar cp.sl 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"];
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




32 min 25 sec ago
11 hours 12 min ago
16 hours 58 min ago
17 hours 16 min ago
19 hours 9 min ago
21 hours 2 min ago
1 day 3 hours ago
1 day 4 hours ago
1 day 6 hours ago
1 day 11 hours ago