nafigator / bash-helpers
Collections of useful functions for usage in Bash scripts.
Installs: 7 184
Dependents: 1
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 0
Open Issues: 0
Language:Shell
README
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
- 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
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)"
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' $?
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.