Previous Next Chapter

Allowing Arguments

The .KEY (or .K) keyword specifies both keyword names and positions in the command line. It teills EXECUTE the number of parameters and how to interpret them. Only one .KEY statement is allowed per script; if present, it should be the first line in the file. Any script containing <$$> must also contain a .KEY statement.

The arguments on the .KEY line can be given with the /A and /K directives, which work the same as in an AmigaDOS template. (Templates are described in Chapter 6.) Arguments followed by /A are required; arguments followed by /K require the name of that argument as a keyword. Use commas to separate multiple arguments in the .KEY line, not spaces.

For example, if a script starts with .KEY filename/A it indicates that a file name must be given on the EXECUTE command line after the name of the script. This file name is substituted in subsequent lines of the script. For example, if you have a script called Newtext and the first line is:

.KEY filename/A, TO/K

you must specify a file name variable. The TO variable is optional, but, if specified, the TO keyword must be used. The following is an acceptable way to run Newtext:

1> EXECUTE Newtext Textfile TO NewFile

Substitution

Before execution, AmigaDOS scans the script for any items enclosed by .BRA and .KET characters (< and > by default). Such items can consist of a keyword or a keyword and a default value. EXECUTE tries to substitute a parameter when it finds a keyword enclosed in angle brackets. However, if you want to use a string in your script file that contains angle brackets or if your script uses angle brackets for redirection of input/output, you must define substitute bracket characters with the .BRA and .KET commands. .BRA <ch> changes the opening bracket character to <ch>, while .KET <ch> changes the closing bracket character to <ch>.

For example, the following script called Demo uses these lines:

. KEY filename
ECHO "This line does NOT print <angle> brackets."
. BRA {
. KET }
ECHO "This line DOES print <angle> brackets."
ECHO "The specified filename is (filename)."

1> EXECUTE Demo TestFile

which results in the following output:

This line does NOT print brackets.
This line DOES print <angle> brackets.
The specified filename is TestFile.

The first ECHO statement causes AmigaDOS to look for a variable to substitute for the <angle> parameter. Since no argument named "angle" is given on the EXECUTE command line, the null string is substituted. The .BRA and .KET commands tell the script to use braces instead of angle brackets to enclose parameters. When the second ECHO statement is executed, the angle brackets - with their special meaning removed - are printed as normal text. The third ECHO statement illustrates that he braces now function as the bracket characters.

Redefine <$$>'s angle brackets with .BRA and .KET to avoid redirection conflicts.

Defaults

When enclosing a keyword in bracket characters, you can also specify a default string to be used if a variable is not supplied on the command line. Defaults can be specified in two ways: every time you reference a parameter or using the .DEF command.

If you specify the default every time you reference a parameter, you must separate the two strings with a dollar sign ($).

For example, in the following statement:

ECHO "<word1$defword1> is the default for Word1."

defword1 is the default value specified for word1. It is printed if no other variable is given for word1. However, if you want to specify this default several times in your script, you must use <word1$defword1> each time.

Defining a default using the .DEF command allows you to specify a default for each specific keyword. For example:

.DEF word1 "defword1"

assigns defword1 as the default for the word1 parameter through the script. The following statement:

ECHO "<word1> is the default for Word1."

results in the same output as the previous ECHO statement:

defword1 is the default for Word1.

The .DOLLAR <ch> command allows you to change the default character from $ to <ch>. (You can also use .DOL <ch>.) For example:

.DOL #
ECHO <word1#defword1> is the default for Word1."

Comments

You can embed comments in a script by including them after a semicolon (;) or by entering a dot (.), followed by a space, then the comment. Blank lines are accepted and ignored within scripts.

Note:

We recommend that you use the semicolon method for entering comments to avoid errors caused by omitting the space following the dot.

Nesting Commands

AmigaDOS provides a number of commands that can be used in scripts, such as IF, ELSE, SKIP, LAB, and QUIT. These commands, as well as the EXECUTE command, can be nested in a script.

To stop the execution of the current script, press Ctrl+D. If you have nested script files, you can stop the set of EXECUTE commands by pressing Ctrl+C.

Example 1:

Assume the script Printit contains the following:

.KEY filename
RUN COPY <filename> TO PRT: +
ECHO "Printing of <filename> done"

The following command:

1> EXECUTE Printit Test/Prg

responds as though you entered the following commands at the keyboard:

1> RUN COPY Test/Prg TO PRT: +
ECHO "Printing of Test/Prg done"

Note the use of the plus sign at the end of the first line. If you press Return after a plus sign, the RUN command also executes the command in the second line when the first command is finished.

Example 2:

Another example, Show, uses more of the features described above:

.KEY name/A
IF EXISTS <name>
TYPE <name> NUMBER ,if file is in the given directory
;type it with line numbers
ELSE
ECHO "<name> is not in this directory"
ENDIF

The command:

1> EXECUTE Show Work/Docfile

displays the Work/Docfile file, with line numbers on the screen, if Show exists in the current directory. If the file is not there, the screen displays an error message. The /A requires a file name be given on the command line after Show or an error occurs.

Interactive Script Files

You can create scripts that pause to request information from the user before continuing. The REQUESTCHOICE or REQUESTFILE commands for creating standard Amiga requesters let you use the familiar Workbench approach to get a user's response. This can accommodate variable conditions with a single script. For example, the following script copies six files from a hard drive to a floppy disk. When the six files are copied, the script asks for confirmation to continue the copy, allowing time to insert a new floppy disk into the disk drive if required.

COPY 2k.eps DF0:
COPY 2m.eps DF0:
COPY 2n.eps DF0:
COPY 2o.eps DF0:
COPY 2t.eps DF0:
COPY 2v.eps DF0:
ECHO "Chapter 2 files copied."
ASK "Continue Copy?"
If WARN
COPY 3a.eps DF0:
COPY 3c.eps DF0:
COPY 3g.eps DF0:
COPY 3aa.eps DF0:
COPY 3bb.eps DF0:
COPY 3ff.eps DF0:
ENDIF

At the "Continue Copy?" prompt, press Y to copy the remaining files to a disk in DF0:. Press N or Return to terminate the copy process.

Top Previous Next Chapter