[SparForte][Banner]
[Top Main Menu] Intro | Tutorials | Reference | Packages | Examples | Contributors   [Back Page]      [Next Page]  

Command Argument Shortcuts

Command line arguments are additional data passed to a script on the command that executes the script. These may include things like filenames and option switches. Although there is a built-in package to handle arguments (command_line), for  compatibility, the familiar Bourne shell syntax can also be used:

  • $? - the status code for the last shell command executed
  • $0 - the script name (not completely implemented)
  • $1...$9 - the first 9 arguments given to this script (if they exist)
  • $# - the number of arguments
  • $$ - the PID of this program, primarily used to uniquely name temporary files

For example,

=> myscript a b c

$1 is "a", $2 is "b" and $3 is "c". The argument variables are always string. The number of parameters, $#, will be 3. Attempting to access an argument that doesn't exist, such as $4 in this example, is an error. Redirection operators are not arguments because they are processed prior to running the command.

If you run script as an argument to the spar command, any arguments occurring after the script name will be treated as the arguments to the script.

For example,

$ spar --trace myscript a b c

In this example, $1 is still "a". The "--trace" argument is not visible to your script.

Here is a longer script example.

-- a short-cut for ls -l for one optional parameter
 
procedure ll is
   dir : string := ".";
begin
  if $# > 0 then
    dir := $1;
  end if;
  ls -l $dir;
end ll;
 

Example: Using Bourne Shell arguments

If there are more than 9 arguments, they can be accessed with the command_line package.

The Bourne shell form are intended as command line shortcuts and the command_line package should normally be used in a well-structured script.

SparForte also permits these command line tokens in dollar expansions. Here is an example of removing a traditional shell temporary file.

rm "/tmp/temp."$$
if $? /= 0 then
   put_line( standard_error, "unable to delete temp file");
end if;
 

Example: Using Bourne Shell arguments - another example

The status code of the last command executed is returned from a script when it finishes executing.  Traditionally, a non-zero status is an error condition.

A SparForte script can set its status code using the command_line package.

Bourne Shell: $@, $# and shift are not available. Use the command_line package for these and loop through the parameters.

 

[Right Submenu]

 AdaScript versus GCC

 Case Sensitivity

 Reserved Words

 Comments

 Literals

 Bourne Shell Word Expansions

 Fundamental Types

 User-defined Types

 Enumerated Types

 Arrays

 Records

 Basic Assignment

 The @ and % Operands

 Command Argument Shortcuts

 Redirection and Pipelines

 Command Line Interaction

 Built-in Shell Commands

 The Current Directory

 Database Commands

 Flow of Control

 Other Statements/ Subprograms

 External Commands

 Block Statements and Subprograms

 TCP/IP Sockets

 Numeric Formatting with Put

 Interpreter Directives

 Command Line Options

 Command Reference

 ASCII and Latin_1 Character Sets

 Common Error Messages

 Common PHP Functions and the SparForte Equivalent

[Back to Top] Back To Top [Small Forte Symbol]