<< Part 1 | Index ^^ | Part 3 >>
Normally a RISC OS command will display any output on the screen, and take input from the keyboard. We can change this using redirection.
*Help Modules { > RAM:$.Modules }
This command saves the list of module titles and versions to a file on the RAM disc. Note that the spacing within the curly brackets is exact, it won’t work if you have too much or little.
*BASIC { < RAM:$.Setup }
This starts the BASIC interpreter, inputting lines from Setup as if you typed them at the keyboard. For example your Setup file might be
TEXTLOAD "RAM:$.Program"
LISTO %1011
LIST
SAVE "RAM:$.Tokenised"
If you try this out, you’ll see that once it’s run out of lines to read from the Setup file, you can type from the keyboard as normal.
These input and output redirections can be combined, in either order.
*BASIC { < RAM:$.Setup > RAM:$.List }
You can use redirection to stop the output of a command appearing
*RMLoad AModule { > null: }
Note that C programs have their own redirection system in addition to that provided by the OS CLI.
Also note that redirection is applied prior to the command-line being processed and is therefore non-conditional. In the following example, two files would be created regardless of the result of the IF statement:
IF "x"="y" THEN ECHO Fail { > RAM:$.out1 } ELSE ECHO Good { > RAM:$.out2 }
The redirection above applies to a single command. There is a different way to apply redirection to several commands at a time.
*Spool RAM:$.Directories
*Dir SDFS::RISCOSpi.$.Scores
*Cat Brahms
*Cat Mozart
*Cat Slade
*Spool
The first *Spool turns on spooling, sending any further screen output to the Directories file instead. The second *Spool with no argument turns off spooling, so screen output goes back to normal.
A system variable has a value assigned using *Set
*Set MyApp$Running Yes
Its value can be used later, by writing the name of the variable in angle-brackets:
If "<MyApp$Running>" = "Yes" Then Echo MyApp is running Else MyApp is not running
will therefore be executed as
If "Yes" = "Yes" Then Echo MyApp is running Else MyApp is not running
Unfortunately it’s not always obvious when a command will handle variable values in its arguments. If it doesn’t, you can use *Do
*Do ACommand <Obey$Dir>
which puts in the value of Obey$Dir, before passing it to ACommand.
As a general rule, variables will always work in filenames.
You can use *Show to find out the value of a variable, or list all the variables currently assigned.
*Show MyApp$Running
*Show *
While you could find out the value of MyApp$Running by
*Echo MyApp$Running
that method will not distinguish between a variable that has been unset:
*Unset MyApp$Running
and one that has been set to a zero-length string:
*Set MyApp$Running ""
Also, it won’t tell you whether a variable is a number or string
*Set Var$1 0
*SetEval Var$2 0
*Echo <Var$1> <Var$2>
0 0
Substitution can be done with Alias$ variables, and inside Obey files. Here’s an example using an alias.
*Set Alias$MyCommand Echo The first argument is '%0', and the second argument is '%1'.
*MyCommand First Second
The first argument is 'First', and the second argument is 'Second'.
The asterisk syntax %*n
means substitute argument n
but also anything after that, so
*Set Alias$MyCommand2 Echo First is '%0', second is '%1', the rest is '%*2'.
*MyCommand2 First Second Third Fourth Fifth
First is 'First', second is 'Second', the rest is 'Third Fourth Fifth'.