nafigator / bash-helpers
Bash 脚本中使用的有用函数集合。
1.1.2
2024-06-11 03:45 UTC
README
bash-helpers
Bash 脚本中使用的有用函数集合
使用方法
不安装
#!/usr/bin/env bash
source <(curl -s https://raw.githubusercontent.com/nafigator/bash-helpers/1.1.1/src/bash-helpers.sh)
安装后
- 将 bash 库放入
/usr/local/lib/bash/includes
目录。 - 在可执行脚本中源
bash-helpers.sh
. /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 安装
composer require nafigator/bash-helpers
功能
-
定义了用于颜色和格式的可读性函数
- black()
- red()
- green()
- yellow()
- blue()
- magenta()
- cyan()
- white()
- gray()
- bold()
- clr()
示例
printf "$(bold)$(red)ATTENTION$(clr) Save $(cyan)failure$(clr)"
注意:出于日志记录目的,颜色可能通过全局
INTERACTIVE
变量被禁用INTERACTIVE=
-
格式化消息的函数
error
、inform
、warning
。示例
inform 'Script start' warning 'Make backup!' error 'File not found'
-
包含的库。
示例
include google/client || exit 1 include mysql/query-builder || exit 1 include logger; status 'Logger including' $? || exit 1
-
状态消息。
示例
test -d /usr/local/nonexistent status 'Check /usr/local/nonexistent dir' $? test -d /usr/local/bin status 'Check /usr/local/bin dir' $?
-
检查依赖项。
示例
check_dependencies yarn rust || exit 1
-
调试消息和状态。
示例
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
版本控制
此软件遵循 "语义化版本控制" 规范。所有声明的公共 API 函数签名。
在 SemVer.org 上了解更多信息。