A unified library to operate on multiple GNU/Linux distributions and using different shells (bash and zsh)

Guillaume Bernard afcca82587 feat: check if a function exists 1 week ago
doc 469b9254d6 fix: use typeset instead of declare for compatibility 3 weeks ago
lib afcca82587 feat: check if a function exists 1 week ago
.gitignore 1edf25e6b4 fix: fix Doxygen documentation 10 months ago
.gitlab-ci.yml 49464a154e fix: fix gitlab-ci.yml 10 months ago
LICENSE b2d834059e Add Licence and README.md 2 years ago
README.md 761b426724 feat: update README.md with better documentation 2 weeks ago
libShell.sh b0882e7e83 fix: replace tabs with 4 spaces 2 weeks ago

README.md

Synopsis

libShell is a simple library that provides interfaces for some use cases such as:

  • Logging messages: through a unique class Logger, you will be able to log every message you want in your program, to the console, a file or both.
  • Package manager abstraction: a unique set a functions will allow you to update index, upgrade packages, add repositories, etc. on your system.
  • And many other functions for: ssh, rsync, git
  • If you’re using both bash and zsh, libShell will work properly.

Use LibShell

Installation on your system

You can use git to install LibShell on your system. This is the recommended mean to get the software. You can also download the archive and extract it at the desired location. This way, you will not be able to installed updates automatically using git. The recommended directory where to store the library is in .local/lib. If you want to install it system-wide, you should prefrer /usr/local/lib

Using git

git clone https://code.guillaume-bernard.fr/guilieb/libShell ~/.local/lib

In order to perform update, you’ll simply have to pull the repository from master.

Use it in your shell scripts

Without using git, you can use *libShell easily, like in the example below:

url=https://code.guillaume-bernard.fr/guilieb/libShell/archive/master.tar.gz; libShell_dir="$(mktemp -d)" && curl -s "${url}" | tar xz --strip 1 -C "${libShell_dir}" && source "${libShell_dir}"/libShell.sh

enableLogger ${ERROR} ${STDOUT}
Log ${NOTE} "Hello from $(getRunningSystemName), version $(getRunningSystemVersion)!"

If you want to stay on a specific version of libShell, you can change the url for something else, like:

https://code.guillaume-bernard.fr/guilieb/libShell/archive/1.0.2.tar.gz

And this prints on my system:

Hello from centos, version 7!

Generate documentation

Documentation is generated using Doxygen. You’ll find the Doxyfile in the doc directory.

Major functionalities

Below are the major functionalities that libShell provides:

Logger interface

Initialization

The logger function is used to send messages to the script stdout. The first step is to enable it, using enableLogger. This function takes to parameters at least. The first is the log level: ${INFO} > ${NOTE} > ${WARNING} > ${ERROR} > ${DEBUG}. The second one is the output: ${STDOUT}, ${FILE}, ${STDOUT_AND_FILE}. If you choose a file, you can pass a suffix for the auto-generated logfile in /tmp.

enableLogger ${ERROR} ${STDOUT}

If you wish to retrieve the logfile:

getLogfile

Log messages

The Log function is used to log messages to the output. You give it a level and a message and it does the rest.

Log ${INFO} "A informative message" # Will be blue
Log ${NOTE} "A note on a spectific point" # Will be green
Log ${WARNING} "A warning on a component" # Will be yellow/orange
Log ${ERROR} "An error occured" # Will be red
Log ${DEBUG} "A debug message" # Will be magenta
Log ${CRITICAL} "A critical error occured, exiting…" # Will have a magenta background

Up to now, every level is redirected to the same output. It is planned to redirect level behind ${WARNING} to /std/stderr by default.

Notes

Redirecting blocs output

{
    Log ${NOTE} "Message"
    ls -l
} > list.txt

The content of the file will exclude the message because the message is sent to /dev/stdout, the standard output for the running process. If you wish to get this output, you need to redirect it manually, to /dev/tty for instance. You can procede as follow:

$ STDOUT_OUT=/dev/tty
$ {
      Log ${NOTE} "Message"
      ls -l
  } > list.txt
Message

Please not that no lmessage is redirected to the file list.txt. Ths one only contains the output of the ls -l command.

Package manager abstraction

libShell provides abstraction for your system package managers: yum, dnf and apt. The operations you can perform are:

  • Update the software cache (like apt update o dnf check-update --refresh)
  updateSoftwareCache
  • Upgrade the system (like apt upgrade or dnf upgrade)
  upgradeSystem
  • Install a list of packages on the system (like apt install or dnf install)
  installSoftwarePackages firefox thunderbird
  • Add a repositoy on your system. On RPM bases distributions using yum or dnf you just need to provide the repository URL. For Debian based distributions, you must give the apt line and the GPG Key URL.
  addRepository "http://www.example.org/archive jessie" "http://www.example.org/gpg-key"

Colors and formatting

You’ll be able to access all colors from terminal (the 16 basic immutable colors) using their pretty name like accessing any other variable: ${BLUE}, ${GREEN}, etc. It is included in the colors.sh file.

It’ the same for formatting output, see formatting.sh.

Licence

libShell is distributed under the GNU GPL v3 license and comes with absolutely no warranty.