Wednesday, July 25, 2012

The Power of Constants

Expect-lite has an old feature, constants, that when I put it in, I only used it occasionally. Constants are immutable, and override variables in the script. This is handy if you want to change the behaviour of the script without going in and editing the script. For example, assume you have the following script called loop.elt:
[ $i < $max
    >echo "hello world"

Running this script would loop send "hello world" 10 times and check for the word "world" each time. But suppose you want to stress things out by running this 10,000 times. Using a constant on the command line when you start the script will do this quickly and easily.
loop.elt max=10000

Note: when using a constant on the command line, don't use the dollar sign ($), it tends to confuse the shell (such as bash).

Of course, you can get into trouble by trying to set a constant for $i, don't do this!
loop.elt i=9

You will end up with an infinite loop, because $i has been defined as a constant, and will always be 9, it can never increment, and never be larger than $max (10). If you wait long enough, you will discover another feature of expect-lite, infinite loop protection. After 5000 iterations (default value), infinite loop protection will kick in and stop the script and print:
ERROR: Infinite Loop Detected!

When creating loops, another trick you may want to use is to begin your script with another variable called $start:

This addition allows you to define $start as a constant, now allowing control via constants of when the loop starts and when it stops (via $max). If for example, you wanted to run the script over the range 3-7, start the script as follows:
loop.elt start=3 max=7

In this post, I have shown how it is possible to override counting variables with constants, but constants are not limited to integers. You can override any variable in your script such as usernames and passwords, or even expected values! Anywhere a variable can be used in expect-lite, it can be overridden by a constant.

Recently, I have begun to use this feature quite a bit. So much so, that I lose track of what variables are in the script, which could be used as constants. So as of version 4.3.0, I added a help feature which lists the variables in the script in "constant format" (without the dollar sign), making it easy to copy and paste into the command line. For example, typing loop.elt -h will print:
Displaying assigned variables, which can be overridden with
a Constant on command line e.g var=value

Constants make it a simple matter to override variables, making your scripts much more flexible, and powerful.

Monday, July 23, 2012

Expect More

Hi All,

This is a blog dedicated to making expect-lite even easier. Sure, anyone can use the simplicity of '>' and '<' in expect-lite, but there is much more.

I will be writing about tips and tricks to help people get the most out of expect-lite, all the while keeping it simple. After all, expect-lite is about automation for the rest of us.

Thanks for checking in, and let me know if you have ideas or topics you want to discuss.

[author and maintainer of expect-lite]