Collection of useful functions for usage in Bash scripts
#!/usr/bin/env bash
source <(curl -s https://raw.githubusercontent.com/nafigator/bash-helpers/1.1.1/src/bash-helpers.sh)
- Put bash libs into
/usr/local/lib/bash/includes
dir. - Source
bash-helpers.sh
in executable script:. /usr/local/lib/bash/includes/bash-helpers.sh
[ -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
#!/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 require nafigator/bash-helpers
-
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)"
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'
-
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' $?
-
Checking dependencies.
Example:
check_dependencies yarn rust || exit 1
-
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' $?
[ OK ] - success status
[FAIL] - fail status
[ ?? ] - debug message
[ ++ ] - success debug status
[ -- ] - fail debug status
This software follows "Semantic Versioning" specifications. All function signatures declared as public API.
Read more on SemVer.org.