nafigator/bash-helpers

Collections of useful functions for usage in Bash scripts.

Installs: 7 188

Dependents: 1

Suggesters: 0

Security: 0

Stars: 4

Watchers: 2

Forks: 0

Open Issues: 0

Language:Shell

1.1.2 2024-06-11 03:45 UTC

README

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.