Name

read-conf — read a shell-style configuration file and chain

Synopsis

read-conf [--oknofile] {filename} {next-prog}

Description

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.

File lexing

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.

Example file

LANG="C"	# Set the LANG variable.
hash=#		# Only the second # on this line begins a comment.
backslash1='\'
backslash2="\\"
backslash3=\\
SHELL=/"bin"/'sh'	# All one value.
greeting="Hello there!"

Examples

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
}

Bugs

As of version 1.34 this program no longer employs the GNU C library nor the BSD C library for environment variable handling. The previously documented workaround for those libraries' having memory leaks in their setenv(3) and clearenv(3) functions, is therefore no longer necessary.

See also

  • appendpath(1)

  • prependpath(1)

  • setenv(1)

  • unsetenv(1)

  • envdir(1)

  • printenv(1)

Author

Jonathan de Boyne Pollard