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 open-source software (New BSD)

Forked from Louwrentius' work

What can we do with it ?

Display well-presented messages

Log easily what your script does

Handle commands' output

Test network information (IPv4, FQDN, ...)

Use timers

Manipulate arrays intuitively

...

Why BSFL ?

No libraries embedded with Bash

Simple tasks can be hard to do with shell scripts

Where to find it ?

https://github.com/SkypLabs/bsfl

How to use it ?

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

Relative path


#!/bin/bash

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

Absolute path


#!/bin/bash

source /opt/bsfl/bsfl.sh
						

Examples

Take a look at the examples directory

Messages

Display a simple message :


msg "I am a message displayed using the function msg."
						
simple message

Messages

With a specific color :


msg "I am a red-colored message displayed using the function msg
with a color parameter." "$RED"
						
message with color

Messages

With a status :


msg_alert "I am an alert message displayed using the
function msg_alert."
						
message with alert status

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 "I am a message logged using the function log."
						
log a simple message

Logs

With a status :


log_critical "I am a critical message logged using the
function log_critical."
						
log a critical message

Logs

Just like for messages, many status are available :

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

Logs

Log functions only work if the log feature is enabled :


LOG_ENABLED=y
						

By default, the messages are logged in 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 log feature is enabled, standard messages are also logged :


LOG_ENABLED=y
msg "Thus, this message is logged in a file."
						
log a standard message

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 status :

simple command

Commands

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


DEBUG=y
cmd "ls -al"
						
command with debug output

Network

Test an IPv4 :


cmd "is_ipv4 8.8.8.8"
						
test an IPv4 address

Network

Test an IPv4 subnet :


cmd "is_ipv4_subnet 192.168.1.0/24"
						
test an IPv4 subnet

Network

Test an FQDN address :


cmd "is_fqdn www.example.net"
						
test a FQDN address

Network

Convert an IPv4 mask into CIDR and vice versa :


mask2cidr 255.255.255.0
cidr2mask 24
						
convert an IPv4 mask into CIDR and vice versa

Many other features !

Take a look at the online Doxygen documentation :

http://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 or FQDN" >> $logfile
		exit 1
	fi
fi
						

With BSFL :


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

Get involved !

Contributions are welcomed