BSFL

Bash Shell Function Library

Created by Paul-Emmanuel Raoul / skyper@skyplabs.net / @SkypLabs

Creative Commons License

What is BSFL?

One-file Bash library

Free and open-source software (New BSD)

Forked from Louwrentius' work

What can we do with it?

Display well-presented messages

Log easily what your scripts do

Handle commands' output

Test network information (IPv4, FQDN...)

Use timers

Manipulate arrays intuitively

...

Why BSFL?

No libraries shipped with Bash

Simple tasks can be difficult to achieve with shell scripts somethimes

Where to find it?

https://github.com/SkypLabs/bsfl

How to use it?

Use Bash 4+ and include BSFL at the beginning of your scripts

Relative path


#!/usr/bin/env bash

declare -r DIR=$(cd "$(dirname "$0")" && pwd)
source $DIR/../lib/bsfl.sh
            

Absolute path


#!/usr/bin/env bash

source /opt/bsfl/bsfl.sh
            

Examples

Take a look at the examples directory

Messages

Display a simple message:


msg "Hello, World!"
            

Messages

With a specific colour:


msg "Hello, World!" "$RED"
            

Messages

With a status:


msg_alert "Hello, World!"
            

Messages

Many status already exist:

  • alert
  • critical
  • debug
  • emergency
  • error
  • failed
  • info
  • not_ok
  • notice
  • ok
  • passed
  • success
  • warning

Logs

Log a simple message:


log "Hello, World!"
            

Logs

With a status:


log_critical "Hello, World!"
            

Logs

Just like for messages, many status are available:

  • alert
  • critical
  • debug
  • emergency
  • error
  • failed
  • info
  • not_ok
  • notice
  • ok
  • passed
  • success
  • warning

Logs

Functions' output is logged only if the logging feature is enabled:


LOG_ENABLED=y
            

By default, log messages are written into a file in the current directory:


declare LOG_FILE="$0.log"
            

For exemple, for test.sh, the log file will be test.sh.log

You can override this behaviour:


LOG_FILE="filename.log"
            

Logs

If the logging feature is enabled, standard messages are also logged:

Commands

Execute a simple command:


cmd "ls -al"
cmd "pnig 8.8.8.8"
            

The function cmd handles the output depending on the command's exit status:

Commands

By default, the command's output is not displayed. To override this behaviour:


DEBUG=y
            

Network

Test an IPv4:


cmd "is_ipv4 8.8.8.8"
            

Network

Test an IPv4 subnet:


cmd "is_ipv4_subnet 192.168.1.0/24"
            

Network

Test an FQDN address:


cmd "is_fqdn www.example.net"
            

Network

Convert an IPv4 mask into CIDR and vice versa:


mask2cidr 255.255.255.0
cidr2mask 24
            

Many other features!

Take a look at the online Doxygen documentation:

https://skyplabs.github.io/bsfl/

Comparison

Without BSFL:


echo $ntp | grep -Eq $regex_ipv4
if [ "$?" -ne 0 ]; then
  echo $ntp | grep -Eq $regex_fqdn
  if [ "$?" -ne 0 ]; then
    echo "NTP address $ntp is not an IPv4 nor an FQDN" >> $logfile
    exit 1
  fi
fi
            

With BSFL:


is_ipv4 $ntp || is_fqdn $ntp
die_if_false $? "NTP address $ntp is not an IPv4 nor an FQDN."
            

Get involved!

Contributions are welcomed