User Login
Site Contents
Script Reference
New User

Contact Me
TWX Scripting Guide, Part 2 - Flexible Scripts
Author: Xide Updated: 9/12/2001

As you've probably guessed, there is really very little that you can do with just waitfor and send commands. Any scripts written using only these two commands won't be flexible enough to be reused without being rewritten for the occasion. So during this tutorial we'll be looking more at commands and options that will let us expand our scripts to make them more much more flexible.

The first thing we'll go over is how to get input from the user and store it in a variable. For this we can make use of the following commands:

echo - Echos text out to all the terminals connected to TWX. This text doesn't get sent to the server, so its useful for putting in script headers and outputting info from scripts.

getConsoleInput - Gets a line of text (or a single key) from the user.

getInput - An older version of getConsoleInput that echos out some text before prompting the user for input.

In most situations, getInput will serve our purposes. Lets just say at the beginning of a PPT script, we want to ask the user which sector they want to PPT with. We could use the command as follows:

getInput $sector "Enter sector to PPT to"

This would display a simple prompt for the user to key something in, then store that value in the $sector variable. We can then use this variable later in our send commands to save us from having to rewrite the script every time we want to trade out a new port pair.

For more information on the echo, getconsoleinput and getinput commands, check the help file.

Its very useful to be able to get script settings or values from the user, but its also limiting to only be able to send or wait for the text as its been typed in. The next thing we'll go over is the IF structure.

Like every other decent scripting language, TWX script has conditions to let you test variables against eachother and other values you may be using. Currently the only condition used (or even needed) in the language is the IF structure. An example of this is shown below:

if ($sector = 1)
echo "*Can't PPT with terra port!"

What this piece of code does is check the $sector variable to see if its equal to 1. If this is the case, we spit out some text at the user and halt the script. In our hypothetical PPT script, this could be useful for checking to see if the user keyed in the terra port by accident.

Anyway, the IF command itself is actually able to handle far more complex conditions than this. If the statement made by the IF command is true, then all the code up to the 'end' command will be processed. If the statement is false, everything within this block of code will be ignored. The IF command has support for the two basic boolean operators: 'or' and 'and'. All the conditions that make up the whole statement can be controlled using brackets. For example:

if (($sector = 1) or ($sector > 5000)) and ($safety = 1)
echo "*Bad sector number entered"

To make the IF structure even more flexible, TWX script has support for 'else' and 'elseif' commands. For example:

if (($sector = 1) or ($sector > 5000)) and ($safety = 1)
echo "*Bad sector number entered"
elseif ($safety = 0)
echo "*Bad sector number entered, but continuing anyway!"
echo "*Starting PPT..."

IF structures are extremely useful when scripting, but we also need to be able to branch our code and make things repeat as necessary. For this, we use the 'goto' command, as shown below.

goto :myLabel

echo "*At my label!"

The 'goto' command will jump to a label in the script matching what is specified. You can indicate a label in your script by putting a column in front of the label name (as shown above). This is a simple idea that can become very powerful when put together with IF structures.

An extension of the 'goto' command is the 'gosub' command. This command works in the same way, although is used instead to jump temporarily to a certain area of the script, then back again with a 'return' command. As shown below:

gosub :mysub
echo "*Got back from my sub"

echo "*Called my sub"

This makes it very easy to split the script into its own subroutines to perform separate tasks, without messing up the logical flow of commands.

Return to Tutorials Page