scripts-common version 2.0.0
This is a free common utilities/tool-box for GNU/Bash scripts, you can use for your own scripts.
scripts-common provides lots of features, using mainly GNU/Bash built-in tools, like:
- logger (writeMessage, info, warning, error), with timestamp and category
- environment check and utilities (locale, isRoot, LSB)
- path check and management (data file, directory, executable)
- configuration file management (local configuration file, global configuration file, check and set configuration)
- version check (isVersionGreater)
- start/stop/up time
- pattern matching (including isNumber, isDate ...)
- extract lines from a file (from N, or between N and P)
- PID file management
- daemon start/pause/stop
- Third party PATH management feature (Java, Ant ...)
Around 2000, I started writing it for my personal needs, creating lots of scripts at home and at work.
In 2019, I extracted the Hemera's utilities part, and enhanced it to get it generic, to share it with everyone.
In all the following methods, you just need to source the utilities file.
source <path to define>/scripts-common/utilities.sh
Method 1 - Git submodule
You can add this project as submodule of your own Git repository.
git submodule add email@example.com:bertrand-benoit/scripts-common.git
And then update your script to use it (e.g. if this script is in the root directory of your repository):
currentDir=$( dirname "$( which "$0" )" ) . "$currentDir/scripts-common/utilities.sh"
Method 2 - Clone repository close to yours
Clone this repository in the parent directory of your own repository.
You can then source the utilities this way:
currentDir="$( dirname "$( which "$0" )" )" source "$( dirname "$currentDir" )/scripts-common/utilities.sh"
Method 3 - Clone repository anywhere
Clone this repository where you want, and define a variable in your
~/.bashrc file, for instance:
Then, in your script, you just need to use this variable:
There are some optional variables you can define before sourcing the
utilities.sh, to tune the system to your needs.
<path>root directory to consider when performing various check
<path>temporary directory where various dump files will be created
<path>directory where PID files will be created to manage daemon feature
<path>path of configuration file to consider
<path>path of GLOBAL configuration file to consider (configuration element will be checked in this one, if NOT found in the configuration file)
0|1activate debug message (not recommended in production)
0|1activate info/debug message
<string>the category which prepends all messages
0|1disable message output on console
<path>path of the log file
0|1activate append mode, instead of override one
0|1check ALL configuration and then quit (useful to check all the configuration you want, +/- like a dry run)
N.B.: when using
checkAndSetConfig function, you can get back the corresponding configuration in LAST_READ_CONFIG variable (if it has NOT been found, it is set to $CONFIG_NOT_FOUND).
The versioning scheme we use is SemVer.
This project is under the GPLv3 License - see the LICENSE file for details