3.5 Statement Lookups

When given more than one argument, cfpeek treats the rest of arguments as search keys. It then searches for statements with pathnames matching each of the keys and outputs them. A key can be either a pathname, or a pattern.

The following command looks for the ‘pidfile’ statement at the topmost level of hierarchy and prints it:

 
$ cfpeek sample.conf .pidfile
.pidfile: /var/run/example

As you see, it uses the same output format as with full listings. If you wish to change it, use the ‘--format’ option, introduced in the previous section. For example, to retrieve only the value:

 
$ cfpeek --format=value sample.conf .pidfile
/var/run/example

This approach is quite common when cfpeek is used in shell scripts. It will be illustrated in more detail below.

If a key is not found, cfpeek prints a message on the standard error and starts searching for the next key (if any). When all keys are exhausted, the program exits with status 1 to indicate that some of them have not been found. To suppress the diagnostics output, use the ‘--quiet’ (‘-q’) option.

To illustrate all this, the following example shows how to use cfpeek in a start-up script to check whether a program has already been started and to bring it down, if requested:

 
#! /bin/sh
pidfile=`cfpeek -q --format=value sample.conf .pidfile`

if test -f $pidfile; then
  pid=`head -1 $pidfile`
else
  pid=
fi

case $1 in
start)  if test -n "$pid"; then
          echo >&2 "the program is already running"
        else
          # start the program
          sample-start
        fi
        ;;
status) if test -n "$pid"; then
          echo "program is running at pid $pid"
        else
          echo "program is not running"
        fi
        ;;
stop)   test -n "$pid" && kill -TERM $pid
        ;;
esac