Skip to content

Collections of handly functions for usage in Bash scripts

License

Notifications You must be signed in to change notification settings

nafigator/bash-helpers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub license GitHub release Conventional Commits Semantic Versioning

bash-helpers

Collection of useful functions for usage in Bash scripts

Usage

Without installation

#!/usr/bin/env bash

source <(curl -s https://raw.githubusercontent.com/nafigator/bash-helpers/1.1.1/src/bash-helpers.sh)

With installation

  1. Put bash libs into /usr/local/lib/bash/includes dir.
  2. Source bash-helpers.sh in executable script:
    . /usr/local/lib/bash/includes/bash-helpers.sh

Installation (optional)

[ -d /usr/local/lib/bash/includes ] || sudo mkdir -p /usr/local/lib/bash/includes
sudo curl -o /usr/local/lib/bash/includes/bash-helpers.sh https://raw.githubusercontent.com/nafigator/bash-helpers/master/src/bash-helpers.sh
sudo chmod +x /usr/local/lib/bash/includes/bash-helpers.sh

Install functions examples

#!/usr/bin/env bash

download_bash_helpers() {
	printf "Installing bash-helpers\n"
	[[ ! -d /usr/local/lib/bash/includes ]] || sudo mkdir -p /usr/local/lib/bash/includes

	sudo curl -so /usr/local/lib/bash/includes/bash-helpers.sh https://raw.githubusercontent.com/nafigator/bash-helpers/master/src/bash-helpers.sh
	sudo chmod +x /usr/local/lib/bash/includes/bash-helpers.sh

	return 0
}

init_bash_helpers() {
	[[ -e /usr/local/lib/bash/includes/bash-helpers.sh ]] || download_bash_helpers

	if [[ ! -x /usr/local/lib/bash/includes/bash-helpers.sh ]]; then
		printf "Insufficient permissions for bash-helpers execute\n"; return 1
	fi

	. /usr/local/lib/bash/includes/bash-helpers.sh

	return 0
}

init_bash_helpers || exit 1

Composer installation

composer require nafigator/bash-helpers

Features:

  • Defines human-readable functions for colors and formatting:

    • black()
    • red()
    • green()
    • yellow()
    • blue()
    • magenta()
    • cyan()
    • white()
    • gray()
    • bold()
    • clr()

    Examples:

     printf "$(bold)$(red)ATTENTION$(clr) Save $(cyan)failure$(clr)"

    Colors definition

    NOTE: For logging purpose colors may be disabled by global INTERACTIVE variable:

    INTERACTIVE=

  • Functions for nicely formatted messages error, inform, warning.

    Examples:

     inform 'Script start'
     warning 'Make backup!'
     error 'File not found'

    Messages formatting

  • Libs including.

    Example:

     include google/client || exit 1
     include mysql/query-builder || exit 1
     include logger; status 'Logger including' $? || exit 1
  • Status messages.

    Example:

     test -d /usr/local/nonexistent
     status 'Check /usr/local/nonexistent dir' $?
     test -d /usr/local/bin
     status 'Check /usr/local/bin dir' $?

    Status messages

  • Checking dependencies.

    Example:

     check_dependencies yarn rust || exit 1

    Check dependencies

  • Debug messages and statuses.

    Example:

     debug 'This message is hidden'
     status_dbg 'This status is hidden' $?
     DEBUG=1
     debug 'Visible because of DEBUG variable'
     test -d /nonexists
     status_dbg 'Visible because of DEBUG variable' $?
     test -d /var/log
     status_dbg 'Visible because of DEBUG variable' $?

    Debug messages

Message statuses

[ OK ] - success status
[FAIL] - fail status
[ ?? ] - debug message
[ ++ ] - success debug status
[ -- ] - fail debug status

Versioning

This software follows "Semantic Versioning" specifications. All function signatures declared as public API.

Read more on SemVer.org.

About

Collections of handly functions for usage in Bash scripts

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages