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.
PATH
environment string, one to a line, on the screen.
AnsABC="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. AnsSian Mountbatten 2012-01-19