a bash library to make life a little bit easier
Report Bug
Β·
Request Feature
Table of Contents
A collection of various functions and scripts for BASH 4.0 or greater:
reduce duplicated code
add your own libraries with ease
- git
mfserv can be installed via a git clone:
git clone https://github.com/mschf-dev/mflibs ./opt/mflibs
mflibs can be sourced by adding the following to your script:
. /path/to/mflibs/src/init
libraries are able to be imported by:
mflibs::import lib1,lib2
the array library handles retrieving information and manipulating information in arrays
check if item is in array
- $1: needle
- $2: haystack
- 0 success
- 1 no match
- 2 missing arguments
declare -a array=("chocolate" "berries" "truffle")
mflibs::array::contains "berries" ${array[@]}
check if array is empty
- $1: array to be checked
- 0 empty
- 2 not empty
declare -a array=("chocolate" "berries" "truffle")
mflibs::array::is_empty "${array[@]}"
join array elements with a delimiter
- $1: glue
- $2: array to be glued
- 0 success
- 2 missing arguments
declare -a array=("chocolate" "berries" "truffle")
mflibs::array::glue "," "${array[@]}"
merge two arrays
- $1: first array
- $2: second array
- 0 success
- 2 missing arguments
declare -a array=("chocolate" "berries")
declare -a array2=("apples" "banana")
mflibs::array::merge "array[@]" "array2[@]"
#example assign:
readarray -t b <<< $(mflibs::array::merge "array[@]" "array2[@]")
the dir library handles directory manipulation
creates and moves into directory
- $1: dir
- 0 success
- 1 mkdir fail
- 2 cd fail
- 3 missing arguments
mflibs::dir::mkcd "newdir"
a library for distro related functions
identify linux codename
function has no arguments
- 0 success
- 1 unable to detect
mflibs::distro::codename
identify linux version
function has no arguments
- 0 success
- 1 unable to detect
mflibs::distro::version
a library for file functions
extracts based on extension
- $1: file to extract
- 0 success
- 1 unable to extract
- 2 invalid amount of arguments
- 3 file does not exist
mflibs::file::extract file.tar.gz
a library for information functions
outputs IPv4 address via dig
- 0 success
- 1 unable to resolve ip
mflibs::info::ipv4::dig
outputs local IPv4 address
- 0 success
- 1 unable to resolve ip
mflibs::info::ipv4::local
a library for logging functions
runs and logs command
- 1: command to log
mflibs::log "echo hi"
a library for shell output functions
NOTE: ALL FUNCTIONS THAT HAVE
mflibs::shell::text
can be appended with colors:mflibs::shell::text::white::bold
is an example
outputs new line
function has no arguments
standard shell output
- $1: string
mflibs::shell::text "hi"
bold shell output
- $1: string
mflibs::shell::text::bold "hi"
underline shell output
- $1: string
mflibs::shell::text::underline "hi"
standout shell output
- $1: string
mflibs::shell::text::standout "hi"
arrow shell functions
- $1: string
mflibs::shell::icon::arrow "hi"
warning shell outputs
- $1: string
mflibs::shell::icon::warning "hi"
check shell outputs
- $1: string
mflibs::shell::icon::check "hi"
cross shell outputs
- $1: string
mflibs::shell::icon::cross "hi"
a library for status handling
outputs error to term
- $1: error message
mflibs::status::error "error_message" 1
outputs error to term and exits with provided return
- $1: error message
- $2: exit code
mflibs::status::kill "error_message" 1
outputs warn to term
- $1: error message
mflibs::status::warn "warning_message" 1
outputs success to term
- $1: success message
mflibs::status::success "success_message"
outputs info to term
- $1: info message
mflibs::status::info "info_message"
a library for verification functions
verifies that arg1 is valid email address
- $1: email
- 0 valid email
- 1 invalid email
- 2 missing arguments
if mflibs::verify::email "test@test.com"; then echo valid; fi
verifies that arg1 is valid ipv6 address
- $1: ipv6 address
- 0 valid ipv6
- 1 invalid ipv6
- 2 missing arguments
if mflibs::verify::ipv6 "2001:db8:85a3:8d3:1319:8a2e:370:7348"; then echo valid; fi
verifies that arg1 is valid ipv4 address
- $1: ipv4 address
- 0 valid ipv4
- 1 invalid ipv4
- 2 missing arguments
if mflibs::verify::ipv4 "192.168.0.1"; then echo valid; fi
verifies that arg1 is alpha
- $1: string to verify
- 0 valid input
- 1 invalid input
- 2 missing arguments
if mflibs::verify::alpha "abc"; then echo valid; fi
verifies that arg1 is alpha-numeric
- $1: string to verify
- 0 valid input
- 1 invalid input
- 2 missing arguments
if mflibs::verify::alpha_numeric "abc123"; then echo valid; fi
verifies that arg1 is alpha-numeric
- $1: string to verify
- 0 valid input
- 1 invalid input
- 2 missing arguments
if mflibs::verify::numeric "123"; then echo valid; fi
verifies that arg1 is alpha (with underscores/dashes)
- $1: string to verify
- 0 valid input
- 1 invalid input
- 2 missing arguments
if mflibs::verify::alpha_dash "abc_"; then echo valid; fi
compares two numbers
- $1: version number to verify
- $2: version number to verify
- 0 equal
- 1 $1 -gt $2
- 2 $1 -lt $2
- 3 missing arguments
- 4 invalid format
mflibs::verify::version "3.0.0" "3.0.1"
echo $?
checks if root or sudo
function has no arguments
- 0 success
- 1 fail
mflibs::verify::sudo
verifies that arg1 is a valid command
- $1: command to verify
- 0 command exists
- 1 command doesn't exist
- 2 missing arguments
mflibs::verify::command "nginx"
verbosity can be enabled on import, there are no additional commands as the libraries will output if it detects the "verbose" lib:
mflibs::import verbose
Contributions are what make the world go around. We would love to be able to accept any new contributions, but I have not written the contribution guidelines yet.
Distributed under the BSD-3-Clause License. See license
for more information.
@ScentedSkunk - Idea & Initial work
- bash-utility - some functions have been used and or adapted from this github
- shields - A service for concise, consistent badges.
- codefactor - Automated code review for GitHub.