LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 176|回复: 0

Shell脚本深入教程:Shell脚本带颜色输出

[复制链接]
发表于 2023-12-13 15:04:41 | 显示全部楼层 |阅读模式

Shell脚本带颜色输出
如果一个脚本功能较多较复杂,那么脚本的人性化输出将显得非常重要。

下面是一个各种带颜色的输出函数,本质都是echo,用法也和echo一样。

#====   Colorized variables  ====
if [[ -t 1 ]]; then # is terminal?
  BOLD="\e[1m";      DIM="\e[2m";
  RED="\e[0;31m";    RED_BOLD="\e[1;31m";
  YELLOW="\e[0;33m"; YELLOW_BOLD="\e[1;33m";
  GREEN="\e[0;32m";  GREEN_BOLD="\e[1;32m";
  BLUE="\e[0;34m";   BLUE_BOLD="\e[1;34m";
  GREY="\e[37m";     CYAN_BOLD="\e[1;36m";
  RESET="\e[0m";
fi

function title() { echo -e "${BLUE_BOLD}# ${1}${RESET}"; }
function finish() { echo -e "\n${GREEN_BOLD}# Finish!${RESET}\n"; exit 0; }
function userAbort() { echo -e "\n${YELLOW_BOLD}# Abort by user!${RESET}\n"; exit 0; }
function warn() { echo -e "${YELLOW_BOLD}  Warning: ${1} ${RESET}"; }
function success() { echo -e "${GREEN}  Success: ${1} ${RESET}"; }
function error() { echo -e "${RED_BOLD}  Error:   ${RED}$1${RESET}\n"; exit 1; }
将上面的内容保存在一个文件中,比如color_print.sh,所有这类工具类的脚本建议都统一存放,比如~/shlibs目录下,以后使用时直接source接口。例如:

#!/bin/bash

source ~/shlibs/color_print.sh

title "Downloading..."
title "Service start..."
success "download file to /tmp"
error "download file to /tmp"
rm "$TARGET_FILE" || warn "delete temp file failed: $TARGET_FILE"
finish
此外,还可以使用一些比较奇特的输出方式:

function throw() { echo -e "[-] FATAL: $1\nexit with code 1"; exit 1; }

echo '[.] copying `FILENAME` ...'
cp /etc/fstab "aaa/" || throw 'Copy `FILENAME` failed!'
echo '[~] copied `FILENAME`'
echo "[+] success: installed to aaa/"

文章链接: https://www.junmajinlong.com/shell/script_course/shell_color/
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表