nafigator / bash-helpers

Bash 脚本中使用的有用函数集合。

安装量: 6,485

依赖者: 1

建议者: 0

安全: 0

星标: 4

关注者: 2

分支: 0

开放问题: 0

语言:Shell

1.1.2 2024-06-11 03:45 UTC

README

GitHub license GitHub release Conventional Commits Semantic Versioning

bash-helpers

Bash 脚本中使用的有用函数集合

使用方法

不安装

#!/usr/bin/env bash

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

安装后

  1. 将 bash 库放入 /usr/local/lib/bash/includes 目录。
  2. 在可执行脚本中源 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)"

    Colors definition

    注意:出于日志记录目的,颜色可能通过全局 INTERACTIVE 变量被禁用

    INTERACTIVE=

  • 格式化消息的函数 errorinformwarning

    示例

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

    Messages formatting

  • 包含的库。

    示例

     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' $?

    Status messages

  • 检查依赖项。

    示例

     check_dependencies yarn rust || exit 1

    Check dependencies

  • 调试消息和状态。

    示例

     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

消息状态

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

版本控制

此软件遵循 "语义化版本控制" 规范。所有声明的公共 API 函数签名。

SemVer.org 上了解更多信息。