Skip to content

Mflibs

CodeFactor Grade Bash Version 4 License GitHub stars GitHub code size in bytes

a bash library to make life a little bit easier
Report Bug ยท Request Feature
explore the docs

Table of Contents
  1. About mflibs
  2. Getting Started
  3. Usage
  4. Libraries
  5. Contributing
  6. License
  7. Authors
  8. Acknowledgments

๐Ÿง About mflibs

A collection of various functions and scripts for BASH 4.0 or greater:

reduce duplicated code

add your own libraries with ease

๐Ÿš€ Getting Started

Prerequisites

  • git

Installation

mfserv can be installed via a git clone:

git clone https://github.com/mschf-dev/mflibs ./opt/mflibs

๐ŸŽˆ Usage

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

๐Ÿ—‚ Libraries

array

the array library handles retrieving information and manipulating information in arrays

mflibs::array::contains()

check if item is in array

arguments:
  • $1: needle
  • $2: haystack
return codes:
  • 0 success
  • 1 no match
  • 2 missing arguments
example:
declare -a array=("chocolate" "berries" "truffle")
mflibs::array::contains "berries" ${array[@]}

mflibs::array::is_empty()

check if array is empty

arguments:
  • $1: array to be checked
return codes:
  • 0 empty
  • 2 not empty
example:
declare -a array=("chocolate" "berries" "truffle")
mflibs::array::is_empty "${array[@]}"

mflibs::array::glue()

join array elements with a delimiter

arguments:
  • $1: glue
  • $2: array to be glued
return codes:
  • 0 success
  • 2 missing arguments
example:
declare -a array=("chocolate" "berries" "truffle")
mflibs::array::glue "," "${array[@]}"

mflibs::array::merge()

merge two arrays

arguments:
  • $1: first array
  • $2: second array
return codes:
  • 0 success
  • 2 missing arguments
example:
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[@]")

dir

the dir library handles directory manipulation

mflibs::dir::mkcd()

creates and moves into directory

arguments:
  • $1: dir
return codes:
  • 0 success
  • 1 mkdir fail
  • 2 cd fail
  • 3 missing arguments
example:
mflibs::dir::mkcd "newdir"

distro

a library for distro related functions

mflibs::distro::codename()

identify linux codename

function has no arguments

return codes:
  • 0 success
  • 1 unable to detect
example:
mflibs::distro::codename

mflibs::distro::version()

identify linux version

function has no arguments

return codes:
  • 0 success
  • 1 unable to detect
example:
mflibs::distro::version

file

a library for file functions

mflibs::file::extract()

extracts based on extension

arguments:
  • $1: file to extract
return codes:
  • 0 success
  • 1 unable to extract
  • 2 invalid amount of arguments
  • 3 file does not exist
example:
mflibs::file::extract file.tar.gz

info

a library for information functions

mflibs::info::ipv4::dig()

outputs IPv4 address via dig

return codes:
  • 0 success
  • 1 unable to resolve ip
example:
mflibs::info::ipv4::dig

mflibs::info::ipv4::local()

outputs local IPv4 address

return codes:
  • 0 success
  • 1 unable to resolve ip
example:
mflibs::info::ipv4::local

log

a library for logging functions

mflibs::log()

runs and logs command

arguments:
  • 1: command to log
example:
mflibs::log "echo hi"

shell

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

mflibs::shell::misc::nl()

outputs new line

function has no arguments

mflibs::shell::output()

standard shell output

arguments:
  • $1: string
example:
mflibs::shell::text "hi"

mflibs::shell::text::bold()

bold shell output

arguments:
  • $1: string
example:
mflibs::shell::text::bold "hi"

mflibs::shell::text::underline()

underline shell output

arguments:
  • $1: string
example:
mflibs::shell::text::underline "hi"

mflibs::shell::text::standout()

standout shell output

arguments:
  • $1: string
example:
mflibs::shell::text::standout "hi"

mflibs::shell::icon::arrow()

arrow shell functions

arguments:
  • $1: string
example:
mflibs::shell::icon::arrow "hi"

mflibs::shell::icon::warning()

warning shell outputs

arguments:
  • $1: string
example:
mflibs::shell::icon::warning "hi"

mflibs::shell::icon::check()

check shell outputs

arguments:
  • $1: string
example:
mflibs::shell::icon::check "hi"

mflibs::shell::icon::cross()

cross shell outputs

arguments:
  • $1: string
example:
mflibs::shell::icon::cross "hi"

status

a library for status handling

mflibs::status::error()

outputs error to term

arguments:
  • $1: error message
example:
mflibs::status::error "error_message" 1

mflibs::status::kill()

outputs error to term and exits with provided return

arguments:
  • $1: error message
  • $2: exit code
example:
mflibs::status::kill "error_message" 1

mflibs::status::warn()

outputs warn to term

arguments:
  • $1: error message
example:
mflibs::status::warn "warning_message" 1

mflibs::status::success()

outputs success to term

arguments:
  • $1: success message
example:
mflibs::status::success "success_message"

mflibs::status::info()

outputs info to term

arguments:
  • $1: info message
example:
mflibs::status::info "info_message"

verify

a library for verification functions

mflibs::verify::email()

verifies that arg1 is valid email address

arguments:
  • $1: email
return codes:
  • 0 valid email
  • 1 invalid email
  • 2 missing arguments
example:
if mflibs::verify::email "test@test.com"; then echo valid; fi

mflibs::verify::ipv6()

verifies that arg1 is valid ipv6 address

arguments:
  • $1: ipv6 address
return codes:
  • 0 valid ipv6
  • 1 invalid ipv6
  • 2 missing arguments
example:
if mflibs::verify::ipv6 "2001:db8:85a3:8d3:1319:8a2e:370:7348"; then echo valid; fi

mflibs::verify::ipv4()

verifies that arg1 is valid ipv4 address

arguments:
  • $1: ipv4 address
return codes:
  • 0 valid ipv4
  • 1 invalid ipv4
  • 2 missing arguments
example:
if mflibs::verify::ipv4 "192.168.0.1"; then echo valid; fi

mflibs::verify::alpha()

verifies that arg1 is alpha

arguments:
  • $1: string to verify
return codes:
  • 0 valid input
  • 1 invalid input
  • 2 missing arguments
example:
if mflibs::verify::alpha "abc"; then echo valid; fi

mflibs::verify::alpha_numeric()

verifies that arg1 is alpha-numeric

arguments:
  • $1: string to verify
return codes:
  • 0 valid input
  • 1 invalid input
  • 2 missing arguments
example:
if mflibs::verify::alpha_numeric "abc123"; then echo valid; fi

mflibs::verify::numeric()

verifies that arg1 is alpha-numeric

arguments:
  • $1: string to verify
return codes:
  • 0 valid input
  • 1 invalid input
  • 2 missing arguments
example:
if mflibs::verify::numeric "123"; then echo valid; fi

mflibs::verify::alpha_dash()

verifies that arg1 is alpha (with underscores/dashes)

arguments:
  • $1: string to verify
return codes:
  • 0 valid input
  • 1 invalid input
  • 2 missing arguments
example:
if mflibs::verify::alpha_dash "abc_"; then echo valid; fi

mflibs::verify::version()

compares two numbers

arguments:
  • $1: version number to verify
  • $2: version number to verify
return codes:
  • 0 equal
  • 1 $1 -gt $2
  • 2 $1 -lt $2
  • 3 missing arguments
  • 4 invalid format
example:
mflibs::verify::version "3.0.0" "3.0.1"
echo $?

mflibs::verify::sudo()

checks if root or sudo

function has no arguments

return codes:
  • 0 success
  • 1 fail
example:
mflibs::verify::sudo

mflibs::verify::command()

verifies that arg1 is a valid command

arguments:
  • $1: command to verify
return codes:
  • 0 command exists
  • 1 command doesn't exist
  • 2 missing arguments
example:
mflibs::verify::command "nginx"

verbose

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

๐Ÿค Contributing

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.

๐Ÿ“ƒ License

Distributed under the BSD-3-Clause License. See license for more information.

โœ๏ธ Authors

@mschf2175 - Idea & Initial work

See the list of contributors who participated in this project.

๐Ÿ“ฃ Acknowledgements

  • 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.

(back to top)