RISC OS Open
Safeguarding the past, present and future of RISC OS for everyone
ROOL
Home | News | Downloads | Bugs | Bounties | Forum | Documents | Photos | Contact us
Account

CLI Basics part 2

<< Part 1 | Index ^^ | Part 3 >>


 

Redirection

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 }

Further redirection

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.

 

System variables

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

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'.

Further reading

  • System Variables in the Programmer’s Reference Manual.

<< Part 1 | Index ^^ | Part 3 >>

Revised on August 19, 2022 10:20:00 by Jon Abbott (1421)? (86.183.16.3)
Edit | Back in time (1 revision) | See changes | History | Views: Print | Source | Linked from: RISC OS CLI, CLI Basics, CLI Basics part 3, CLI Basics part 1

Search the Wiki

Social

Follow us on and

ROOL Store

Buy RISC OS Open merchandise here, including SD cards for Raspberry Pi and more.

Donate! Why?

Help ROOL make things happen – please consider donating!

RISC OS IPR

RISC OS is an Open Source operating system owned by RISC OS Developments Ltd and licensed primarily under the Apache 2.0 license.

Navigation

  • Home Page
  • All Pages
  • Recently Revised
  • Authors
  • Feeds
  • Export
Site design © RISC OS Open Limited 2018 except where indicated
The RISC OS Open Instiki theme is based on Insitki's default layout

Valid XHTML 1.0  |  Valid CSS

Instiki 0.19.1(MML+)
This site runs on Rails

Hosted by Arachsys