read-conf — read a shell-style configuration file and chain
read-conf
[--oknofile] {filename
} {next-prog
}
read-conf reads the file filename
, constructing a sequence of environment variable assignments, and then attempts to chain load next-prog
with the execvp(3) function.
Unless the [--oknofile] command line option is given, then a failure to open the file for reading is a fatal error.
With that option, the failure will be ignored and next-prog
chain loaded.
read-conf implements a very limited subset of the sh(1) lexical syntax for shell script files.
The configuration files that it reads are usually shell script files that consist solely of simple variable assignments.
Variable, command, brace, and parameter substitution are generally not used and are not supported by read-conf.
(This limited syntax is actually a requirement for the likes of /etc/rc.conf
on OpenBSD, where it is mandated that system configuration files be parseable by things that are not shell script interpreters.
/etc/os-release
and /etc/machine-info
on Linux operating systems have a similar requirement not to perform substitutions.)
Each assignment, once read, is split at the first equals character into a variable name and its value. read-conf sets the environment variable named to the value given.
Note that assigning an empty value to a variable is not the same as unsetting the variable.
If either the variable name or its value contain an equals character, counter-intuitive things will happen to the environment.
Assignments are normally separated by one or more whitespace characters. Pairs of quotation mark characters will prevent whitespace from being treated specially, and the quoted sequence of characters, which can include newlines, will form part of one assignment, concatenated with whatever quoted or unquoted sequences precede or follow it.
Inside a double-quotation mark delimited sequence of characters: A slash followed by a newline is discarded entirely. A slash followed by any other character is that character. This allows one to provide literal slashes and quotation marks.
In a non-quotation mark delimited sequence of characters: A slash followed by a newline is discarded entirely. A slash followed by any other character is that character. This allows one to provide literal slashes, hashes, and quotation marks. A hash at the start of a sequence is a line comment, that is discarded along with everything that follows it up to and including the end of the line.
Inside a single-quotation mark delimited sequence of characters no characters are discarded or treated specially, except of course the single quotation mark that terminates the sequence.
This handy shell function reads system information from some conventional information files, that are defined to contain simple variable assignments without shell expansions or substitutions.
It takes advantage of the fact that these are all built-in commands (c.f. exec(1)) to completely clear the environment including PATH
before reading the information files into it.
read_os () {
if test -r /etc/os-release
then
clearenv setenv "$1" "$2" read-conf /etc/os-release printenv "$1"
else
clearenv setenv "$1" "$2" read-conf /usr/lib/os-release printenv "$1"
fi
}