shell脚本之颜色
来源:互联网 发布:mac文件复制快捷键 编辑:程序博客网 时间:2024/06/06 17:07
当我们书脚本的时候,会想要把其中的某些部分进行重点的提示,这就涉及到怎么把字符标记显示不同的颜色。
常用的颜色有:
黑色\033[30m xxx \033[0m红色\033[31m xxx \033[0m绿色\033[32m xxx \033[0m黄色\033[33m xxx \033[0m蓝色\033[34m xxx \033[0m紫色\033[35m xxx \033[0m天蓝色\033[36m xxx \033[0m白色\033[37m xxx \033[0m 注:xxx是自己想输入的任意字符但是这些颜色所对应的代码很难记,所以我们可以将这些数字用变量来定义,写入一个脚本color.sh:
#!/bin/sh#Date: xx-xx-xx#Author: guang#Mail: canyudeguang@163.com#functions: color variablesBLACK='\033[30m'RED='\033[31m'GREEN='\033[32m'YELLOW='\033[33m'BLUE='\033[34m'PINK='\033[35m'END='\E[0m'在需要用的时候加载进自己的脚本即可:
#!/bin/sh#Date: xx-xx-xx#Author: guang#Mail: canyudeguang@163.com#functions: test colors[ -f ~/clolor.sh ] && . ~/color.sh || exit 1echo -e "${RED} who are you? ${END}"echo -e "${YELLOW} welcome to my blog ${END}"当然,更恰当的做法是将这些变量定义成一个函数进行调用:
#!/bin/sh#Date: xx-xx-xx#Author: guang#Mail: canyudeguang@163.com#functions: color variablesaddColor(){BLACK='\033[30m'RED='\033[31m'GREEN='\033[32m'YELLOW='\033[33m'BLUE='\033[34m'PINK='\033[35m'END='\E[0m'[ $# -ne 2 ] && { echo "Usage: $0 content color." exit 1}case "$2" in black|BLACK) echo -e "${BLACK}$1${END}" ;; red|RED) echo -e "${RED}$1${END}" ;; green|GREEN) echo -e "${GREEN}$1${END}" ;; yellow|YELLOW) echo -e "${YELLOW}$1${END}" ;; blue|BLUE) echo -e "${BLUE}$1${END}" ;; pink|PINK) echo -e "${PINK}$1${END}" ;; *) echo -e "${RED}$1${END}"esac}
加载改脚本,调用方式为:
addColor "welcome to my blog." blue
0 0
- shell脚本之颜色
- shell脚本之颜色
- shell脚本显示颜色
- linux shell 脚本的颜色
- shell脚本显示字体颜色
- shell脚本颜色打印输出
- shell 脚本设置文本颜色
- shell脚本输出颜色字体
- shell脚本输出颜色字体
- shell脚本颜色打印输出
- shell脚本之shell数组
- 设置shell脚本输出字体颜色
- Shell与Perl脚本中的颜色控制
- shell 脚本中字体颜色的显示
- shell脚本中echo打印带颜色
- SHELL脚本的echo输出颜色
- shell脚本中echo带颜色输出
- shell脚本输出带颜色字体
- ORACLE分组查询和统计等
- 机器人工程师学习计划
- Java 编程规范
- HashMap HashTable ConcurrentHashMap区别
- 我的第一个html
- shell脚本之颜色
- IOS开发笔记8-C语言基础复习
- Android 如何设置让TextView文本可被选择
- MIT牛人解说数学体系
- 笔记(1)
- jquery:prop()
- 404等错误页面跳转问题--基于struts2和原生
- POJ 3468 A Simple Problem with Integers(线段树区间更新模板题)
- Java编程军规