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:
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.
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:
Git like
$ myscript config server.host example.org $ myscript config server.proxypath /home/johndoe/proxy $ myscript config server.httppath /home/johndoe/webInteractive
$ 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/webgetopts with long options
$ myscript --host example.org --proxypath /home/johndoe/proxy --httppath /home/johndoe/webSimple
$ 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?