Open Source

Scripting in Linux with execline

Vincent Danen describes an alternative to traditional shell scripts, like bash. If you are resource-conscious, you may want to consider execline scripting as a leaner, more secure, and faster scripting method.

Most individuals write shell scripts in bash, or "sh" (which is often, but not always, bash). Other shells are more than capable of executing shell scripts as well, but the problem with shell scripts executed by shells is they can take more memory than they should, usually because if you execute a shell script from inside a shell, you now have two shells running instead of one. For example, if you ran a shell script and looked at the output of ps as it was executing, you might see something like:

25024 pts/0  Rs   0:00 -zsh
25036 pts/0  T   0:00 /bin/sh ./test.sh
25037 pts/0  T   0:00 sleep 100

There are alternatives to bash and similar shells for shell scripting, particularly if resources are an issue. One such alternative is execline. execline is a non-interactive, secure scripting language with a very different syntax from traditional shells. It's very simple and straightforward, secure, and portable. It's also very fast.

For instance, if the above output was from running the script test.sh, which contained:

#!/bin/sh
sleep 100

The execline equivalent would be:

#!/bin/sh
/bin/foreground { /bin/sleep 100 }

But notice that, unlike /bin/sh, the execline program doesn't hang around:

16183 pts/0  Rs   0:00 -zsh
16247 pts/0  T   0:00 /bin/sleep 100

This is because execline creates a chain of events, so to speak, and then exits. Each line in an execline script takes the output of the previous command as input to the next command. Once execline has parsed the script, it starts the chain of events and then exits. Although execline works very well for many types of scripts, it does have a few limitations. It won't work in every situation for which you would need to write a script (see the Current Limitations section at http://www.skarnet.org/software/execline/execline.html).

When combining execline with a tool such as runit, system startup becomes quite quick with low overhead. And the execline scripts aren't overly complex, once you get used to them. As an example, examine the stage 1 runit script as provided in Annvix in Listing A.

As you can see, each command in the script is an actual executable. Here, the /bin/foreground program is a simple program that reads the program within the block (such as /sbin/rc), forks, executes the program, and waits for it to complete; then, it execs into the next program noted outside the block.

In addition, using execline doesn't preclude the use of shell testing, but instead of writing:

if [ -f /sbin/runit.old ]; then
  rm -f /sbin/runit.old
fi

You're using:

/bin/foreground {
  /bin/if {
    /usr/bin/test -f /sbin/runit.old
  }
  /bin/rm -f /sbin/runit.old
}

Execline is a great scripter's tool, particularly for those working with numerous scripts or scripts that can execute many times (at the same time), or even those working in embedded environments.

Delivered each Tuesday, TechRepublic's free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

Editor's Picks