Let’s imagine we have some script ‘m12’ (I’ve just invented this name) that runs
on Linux computers. If it is situated in your $PATH, you can easily run it
from the console like this:
m12
It will work with the default parameters. But you can customize the work of
this script by running it something like:
m12 --enable_feature --select=3
It is great and it will work. But I want to create a config file ~/.m12rc so I
will not need to specify --enable_feature --select=3 every time I run it.
It can be easily done.
The difficult part is starting here.
So, I have ~/.m12rc config file, but I what to start m12 without parameters that
are stored in that config file. What is the Unix way to do this? Should I run
script like this:
m12 --ignore_config
or there is better solution?
Next. Let’s imagine I have a config file ~/.m12rc and I want some parameters from that
file, but want to change them a bit. How should I run the script and how the
script should work?
And the last question. Is it a good idea for script to first look for .m12rc
in the current directory, then in ~/ and then in /etc?
I’m asking all these questions because I what to implement config files in my
small script and I want to make the correct decisions about the design.
The book ‘The Art of Unix Programming’ by E S Raymond discusses such issues.
You can override the config file with
--config-file=/dev/null.You would normally use the order:
/etc/m12/m12rc, or just/etc/m12).~/.m12rc)./.m12rc)with each later-listed item overriding earlier listed items. You should be able to specify the configuration file to read on the command line; arguably, that should be given precedence over other options. Think about
--no-system-configor--no-user-configor--no-local-config. Many scripts do not warrant a system config file. Most scripts I’ve developed would not use both local config and user config. But that’s the way my mind works.The way I package standard options is to have a script in
$HOME/bin(saym12a) that does it for me:If I want those options, I run
m12a. If I want some other options, I run rawm12with the requisite options. I have multiple hundreds of files in my personal bin directory (about 500 on my main machine, a Mac; some of those are executables, but many are scripts).