一个现代 Bash 脚本模板,具有彩色输出格式和实用函数,用于创建专业的命令行工具。
chmod +x script.sh
./script.sh
#!/bin/bash
LIB="lib"
source $LIB/format.sh
section "Your Script Section"
info "Information message"
ok "Success message"
warn "Warning message"
error "Error message"
info "message" - 显示带有青色 [i] 前缀的信息消息ok "message" - 显示带有绿色 [+] 前缀的成功消息warn "message" - 显示带有黄色 [!] 前缀的警告消息error "message" - 显示带有红色 [-] 前缀的错误消息(发送到 stderr)prompt "question" - 显示带有洋红色 [?] 前缀的提示,并等待用户输入section "title" - 创建一个格式化的部分标题bold "text" - 以粗体格式显示文本以下颜色常量可用于自定义格式:
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
BOLD='\033[1m'
UNDERLINE='\033[4m'
RESET='\033[0m'
当您运行示例脚本时,您将看到:
=== Start ===
[i] Running exploit setup...
[+] Everything is ready
[!] This might break your system
[-] Something failed
[?] Continue? [y/N]:
shell_template/
├── script.sh # Example script demonstrating usage
├── lib/
│ └── format.sh # Core formatting library
├── LICENSE # MIT License
└── README.md # This file
您可以在 lib/format.sh 中添加新的消息类型来扩展库:
DEBUG="${BLUE}[D]${RESET}"
debug() { echo -e "${DEBUG} $*"; }
通过修改颜色常量或创建新颜色来定义您自己的配色方案:
PURPLE='\033[0;35m'
CUSTOM_PREFIX="${PURPLE}[*]${RESET}"
custom() { echo -e "${CUSTOM_PREFIX} $*"; }
source $LIB/format.shsection 来组织脚本输出error 并重定向到 stderrchmod +x欢迎提交 issue、fork 仓库,并为任何改进创建拉取请求。
本项目采用 MIT 许可证 - 有关详细信息,请参阅 LICENSE 文件。
由 Christoph Huy 创建