Previous Next Chapter

Advanced Tasks

The following examples illustrate advanced tasks for users who are quite familiar with AmigaDOS.

Testing Software Versions

You might need to control what a script does depending on which software version a user of the script has. It is easy to test for specific version numbers from a script.

To test whether a script is running on an Amiga with Release 2 level system software, preface the version-dependent portion of the script with a sequence similar to this:

VERSION >NIL: 37
IF WARN
ECHO "It's really time to update your system."
QUIT
ELSE
ECHO "You have Release 2 or better. Good!"
ECHO "Let's continue."
ENDIF

Versions below 37 are pre-Release 2, version 38 is Release 2.1, version 39 is Release 3, version 40 is Release 3.1 and version 44 is Release 3.5.

Flushing Unused Fonts and Libraries

When fonts and libraries are loaded into memory, they remain in memory even if they are not currently in use. They are removed from memory automatically only when the memory they occupy is needed for some other purpose. In some cases it is useful to remove unneeded resources yourself.

For example, if you want to edit and then test a font or find the version number of a library on a new disk, the Amiga may appear not to see the new font or library. This is because AmigaDOS uses the font or library that is in memory whenever possible. You can flush unused resources from memory and decrease memory fragmentation by using the AVAIL command's FLUSH option.

To flush an unused font or library from memory without having to reboot the Amiga, enter:

1> AVAIL FLUSH

For this command to eliminate a font or library, the library or font must not be in use by the Workbench or some other application.

AmigaDOS Loops Using EVAL

To create a loop in AmigaDOS that can prompt for the number of times to loop:

Enter the following script and save it as Aloop:

.KEY loop
; change bracket characters used for substitution
; since script uses < and > for redirection:
.BRA {
.KET }
; test whether user provided an argument
; for the number of loops, prompt if not:
IF NOT {loop}
ECHO "Please type in the number of loops"
ECHO "and press Return: " NOLINE
SETENV >NIL: loop{$$} ?
ELSE
; there was an argument, so store its value
ECHO >ENV:Loop{$$} {loop}
ENDIF
;
LAB start ; top of loop
ECHO "Loop #" NOLINE ; here, substitute the
TYPE ENV:Loop{$$} ; commands to repeat
EVAL <ENV:Loop{$$} >NIL: TO=T:Qwe{$$} VALUE2=1
OP=- ?
TYPE >ENV:Loop{$$} T:Qwe{$$}
IF VAL $loop{$$} GT 0
SKIP start BACK ;loop not finished yet
ENDIF
;
DELETE ENV:loop{$$} T:Qwe{$$} QUIET ; clean up
ECHO "Done"

If you invoke this script without providing a number as an argument, you are asked for input and this value is used as the initial loop number. If you do provide a number, as in:

1> EXECUTE Aloop 5

the following results are displayed:

Loop #5
Loop #4
Loop #3
Loop #2
Loop #1
Done

The only action inside the loop is to display the current loop count. However, you can insert more meaningful actions using the Loop{$$} environment variable.

The first IF block checks whether an argument was given when the script was invoked. If not, the script prompts for a value. In either case, the script stores the value in the ENV:Loop{$$} variable file. The {$$} operator appends the process number to the name Loop to create a unique file name to avoid potential conflicts while multitasking. For example, the file name in ENV: might be Loop4.

Within the loop, an ECHO command coupled with a TYPE command displays Loop # followed by the number given as the loop argument. The first time through the loop, it displays Loop #5 .

The EVAL command takes the number in the ENV:Loop{$$} file as <value1>, making the question mark at the end of the line necessary. <Value2> is 1 and the operation is subtraction. The output of the EVAL command is sent to the T:Qwe{$$} file. The next TYPE command sends the value in T:Qwe($$) to the ENV:Lopp{$$} file. The effect of these two lines is to subtract one from the value in ENV:Loop{$$}.

The IF statement instructs the script to start over as long as the value for Loop{$$} is greater than 0. This results in the Loop # line being printed again showing the new value.

The script continues until Loop{$$} is equal to 0. At the end of the script, the two temporary files are deleted.

Using PIPE:

To get a listing of one device's contents to another process:

From process 1:

1> LIST Work: TO PIPE: ALL

From process 2:

2> TYPE pipe:

To gather the results of several C compilations:

1> sc >pipe:11 milk.c
1> sc >pipe:11 snap.c
1> sc >pipe:11 crackle.c
1> sc >pipe:11 pop.c
1> TYPE pipe:11

To use channel names:

1> LIST >pipe:crazy

This lists to a pipe called "crazy".

1> COPY #?.c TO >pipe:all_c/32000

This specifies a channel called "all_c" and a buffer size of 32000 bytes.

To set a limit on the number of buffers to 5:

1> DIR >pipe://5

This creates a channel without a channel name and allows only 5 buffers.

Recursive AmigaDOS Command Scripts

To create a script that allows you to apply any AmigaDOS command to the contents of a directory, including all its subdirectories and their contents, enter this script and save it as S:RPAT:

.KEY COM/A,PATH,OPT,RD
; enter the command for COM, enter the path to the
;directory tree to process for PATH, if it is not
;the current directory, and enter an option for
;the command as OPT. You do not need to enter
;anything for RD.
;
.BRA {
.KET }
FAILAT 21 ; do not sdtop when List finds nothing
;
; first scan for files, then generate new script:
LIST >T:trf{$$} "{PATH}" FILES LFORMAT=" {COM}
*"%p%n*" {OPT}"
IF EXISTS T:trf{$$}
; files were found, execute the new script and
; clean up
EXECUTE Z:trf{$$}
DELETE T:trf{$$}
ENDIF
;
;list any subdirectories, call RPAT for each one:
LIST >T:trd{$$} {RD} "{PATH}" FIRS LFORMAT="RPAT
*"{COM}*" *"%p%n*" *"{OPT}*" RD=.{RD}"
;the RD argument appends periods to the names of
;the temporary script files to distinguish each
;level
;
IF EXISTS T:trd{$$} {RD}
; subdirectories exist, execute new script
; file and clean up
EXECUTE T:trd{$$} {RD}
DELETE T:trd{$$} {RD}
ENDIF

Make sure the RPAT script has its a protection bit set and that RPAT is in the search path when you run it. Examine the S:SPAT and S:DPAT scripts that come with your system for examples of similar techniques.

Top Previous Next Chapter