Subsections

- Exercises

# Environment strings

In Linux, if, at the command prompt in a Bash shell, you key `set` followed by return, you will get a listing of the values of all the environment strings defined in your session. The value of the environment string `PATH` gives all the paths that the operating system will search when you try to execute a program.

Each string is identified by what is called an environment variable which behaves rather like a name of mode `REF STRING` except that each string is terminated with a `nul ch`. You can open a book containing the environment string using `env channel`. For example:

       FILE p;  open(p,"PATH",env channel)

The open will fail if `PATH` has not been defined, so a plain `open` (as shown in the above example) would be better replaced by

       FILE p;
       IF open(p,"PATH",env channel)/=0
       THEN #code to take emergency action#
       ELSE #code to perform the usual actions#
       FI

If you now use `make term` to make the colon `:` the string terminator, you can get the individual paths using `get`:

       make term(p,":"+nul ch);
       STRING path;
       on logical file end(p,
        (REF FILE f)BOOL:
         (GOTO eof;  SKIP));
       DO
          get(p,(skip terminators,path));
          IF UPB path >= LWB path
          THEN write((path,newline))
          FI
       OD;
       eof:
       close(p);

You should close the book after using it. Notice the use of a `GOTO` followed by a label. The actual label, which looks just like an identifier. is followed by a colon.

## Exercises

9.10
  Write a program which will display the individual paths in the `PATH` environment string, one to a line, on the screen. Ans
9.11
  Write a program which will read some arguments from its command line, each argument consisting of the identifier of an environment string terminated by "/" followed by a non-blank terminator. Using this data, read the environment string and display its constituent parts on the screen, one to a line. Allow for the possibility that the string might not end with the terminator (the code given in the answer caters for that possibility). Try an environment string which exists and one which doesn't. Ans
9.12
  At the command line, by using the command

         ABC="12 14 16"

  you create (using `bash`) an environment string identified by `ABC`. Now write a program which will read the individual numbers from `ABC` and print their total. Try changing the value of `ABC` to give different numbers (not in the program). Include a test in your program to determine whether `ABC` is present in the environment (verb|open| will fail if it isn't) and terminate your program with a useful message if not. Ans