Shell Scripting – Best Practices for User Configuration of a Shell Script

bashconfigurationconfiguration-managementshell

I am writing a shell script with a few variables that should be configured by the user. There will be an installer for downloading and configuring the script, possibly by asking a series of question. The script in question is aimed at other developers.

This can be implemented in a number of ways:

  1. Use placeholders in the script itself and use sed to replace them during installation (something like this: https://stackoverflow.com/questions/415677/how-to-replace-placeholders-in-a-text-file)

    • Pros: All variable definitions are contained within the script. It's easy to download the script manually and configure the variables for users who prefer an editor over the installer.

    • Cons: It's hard to reconfigure the variables through the installer once they are in place. Unless I create a more complex regexp which would be prone to errors.

  2. Use a config file, basically another shell script with assignments, and use source to include it. (And probably place it in ~/.scriptname? The main script is copied to /usr/local/bin)

    • Pros: It's easy to reconfigure the script. Could even add a parameter for doing so from the main script (Would probably work in the first solution as well, but editing a script from itself doesn't sound like a very good idea)

    • Cons: The script is now dependent on two files and the user is required to run the installer for the configuration file to be created. This can be solved by auto generating a config file if none exists. But locating an external configuration file will still be more cumbersome for users who just want to download the script, edit it, and be done with it.

Also, a few options regarding how the configuration should be managed by the user after the installation:

  1. Git like
    $ myscript config server.host example.org
    $ myscript config server.proxypath /home/johndoe/proxy
    $ myscript config server.httppath /home/johndoe/web

  2. Interactive
    $ myscript config
    Enter the server hostname: example.org
    Enter the path to the proxy on the server: /home/johndoe/proxy
    Enter the path to the http directory on the server: /home/johndoe/web

  3. getopts with long options
    $ myscript –host example.org –proxypath /home/johndoe/proxy –httppath /home/johndoe/web

  4. Simple
    $ myscript config example.org /home/johndoe/proxy /home/johndoe/web

Are there any other ways of doing this that you would consider?
Any best practices, anything elegant?

Best Answer

What would I expect from a sane program (a shell script or not):

  • I never ever have to alter the executable to just configure it. This is not an OS kernel.
  • I can pass any setting using command line. This is a must for every piece of information that does not have a reasonable default. The only exception is a password which needs interactive input.
  • Optionally, I can pass a setting using an environment variable.
  • I can write the settings down into a config file, and this file will be used if it is present under a well-known name, or explicitly pointed to using the two methods above.
  • The config file uses the very same setting names and syntax as command line.
Related Topic