BSFL – Bash Shell Function Library – How to use it ?



BSFL – Bash Shell Function Library – How to use it ?

0 0


bsfl-slideshow

Slide show for introducing the BSFL project

On Github SkypLabs / bsfl-slideshow

BSFL

Bash Shell Function Library

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

What is BSFL ?

One-file Bash library

Free Open Source Software (New BSD)

Forked from Louwrentius' work

What can we do with that ?

Display well-presented messages

Log easily what your script does

Handle commands' output

Test network informations (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 script

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 "This is a classic displayed message using 'msg' function."

Messages

With a specific color :

msg "This is a red displayed message using 'msg' function
with color parameter." "$RED"

Messages

With a status :

msg_alert "This is a displayed message with its status using
'msg_alert' function."

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 "This is a simple message to log."

Logs

With a status :

log_critical "This is a logged message using 'log_critical'
function."

Logs

Just like for messages, many status are available :

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

Logs

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 overwrite this behavior :

LOG_FILE="filename.log"

Logs

You can even log standard messages :

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

Commands

Execute a simple command :

cmd "ls -al"
cmd "pnig 8.8.8.8"

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

Commands

By default, the command's result is not displayed. To overwrite this behavior :

DEBUG=y
cmd "ls -al"

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

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

BSFL Bash Shell Function Library Created by Paul-Emmanuel Raoul / skyper@skyplabs.net / @SkypLabs