Bash echo输出带颜色和背景的文本
Bash echo输出带颜色和背景的文本
1、先上效果图
2、bash代码
#!/bin/bash
#*************************************************************
#Author: yangruitao
#Date: 2020-11-04
#FileName: color.sh
#*************************************************************
black="0"
red="1"
green="2"
yellow="3"
blue="4"
magenta="5"
cyan="6"
white="7"
# Color 为文本和背景设置颜色
function Color() {
Content=""
Fg="3$1"
Bg="4$2"
SetColor="\e[$Fg;${Bg}m "
EndColor=" \e[0m"
for((i=3;i<=$#;i++)); do
j=${!i}
Content="${Content} $j "
done
echo -e ${SetColor}${Content}${EndColor}
}
# echo_black 输出黑色文本 可加背景颜色参数(背景默认不设置)
function echo_black() {
if [ "$1" == "-b" ]; then
Bg=$(($2))
Content=$3
else
Bg="8"
Content=$1
fi
Color $black $Bg $Content
}
# echo_red 输出红色文本 可加背景颜色参数(背景默认不设置)
function echo_red() {
if [ "$1" == "-b" ]; then
Bg=$(($2))
Content=$3
else
Bg="8"
Content=$1
fi
Color $red $Bg $Content
}
# echo_green 输出绿色文本 可加背景颜色参数(背景默认不设置)
function echo_green() {
if [ "$1" == "-b" ]; then
Bg=$(($2))
Content=$3
else
Bg="8"
Content=$1
fi
Color $green $Bg $Content
}
# echo_yellow 输出黄色文本 可加背景颜色参数(背景默认不设置)
function echo_yellow() {
if [ "$1" == "-b" ]; then
Bg=$(($2))
Content=$3
else
Bg="8"
Content=$1
fi
Color $yellow $Bg $Content
}
# echo_blue 输出蓝色文本 可加背景颜色参数(背景默认不设置)
function echo_blue() {
if [ "$1" == "-b" ]; then
Bg=$(($2))
Content=$3
else
Bg="8"
Content=$1
fi
Color $blue $Bg $Content
}
# echo_magenta 输出洋红色文本 可加背景颜色参数(背景默认不设置)
function echo_magenta() {
if [ "$1" == "-b" ]; then
Bg=$(($2))
Content=$3
else
Bg="8"
Content=$1
fi
Color $magenta $Bg $Content
}
# echo_cyan 输出青色文本 可加背景颜色参数(背景默认不设置)
function echo_cyan() {
if [ "$1" == "-b" ]; then
Bg=$(($2))
Content=$3
else
Bg="8"
Content=$1
fi
Color $cyan $Bg $Content
}
# echo_white 输出白色文本 可加背景颜色参数(背景默认不设置)
function echo_white() {
if [ "$1" == "-b" ]; then
Bg=$(($2))
Content=$3
else
Bg="8"
Content=$1
fi
Color $white $Bg $Content
}
#main
echo -e "example ... [\e[1;30m black \e[0m|\e[1;31m red \e[0m|\e[1;32m green \e[0m|\e[1;33m yellow \e[0m|\e[1;34m blue \e[0m|\e[1;35m magenta \e[0m|\e[1;36m cyan \e[0m|\e[1;37m white \e[0m]"
echo -en "using [\e[1;40m echo_black \"hello\"\e[0m ] to output black text: "
echo_black "hello"
echo -en "using [\e[1;41m echo_red \"hello\" \e[0m] to output red text: "
echo_red "hello"
echo -en "using [\e[1;42m echo_green \"hello\" \e[0m] to output green text: "
echo_green "hello"
echo -en "using [\e[1;43m echo_yellow \"hello\" \e[0m] to output yellow text: "
echo_yellow "hello"
echo -en "using [\e[1;44m echo_blue \"hello\" \e[0m] to output blue text: "
echo_blue "hello"
echo -en "using [\e[1;45m echo_magenta \"hello\" \e[0m] to output magenta text: "
echo_magenta "hello"
echo -en "using [\e[1;46m echo_cyan \"hello\" \e[0m] to output cyan text: "
echo_cyan "hello"
echo -en "using [\e[1;47m echo_white \"hello\" \e[0m] to output white text: "
echo_white "hello"
echo -en "using [\e[30;47m echo_black -b white \"hello,world!\" \e[0m] to output black text with white background: "
echo_black -b white "hello, world!"
3、其他脚本中使用示例
example.sh
#!/bin/bash
#导入color.sh脚本,即可调用里面的函数(若exmaple.sh与color.sh不在同一目录,下面的导入记得使用color.sh的绝对路径)
. color.sh
echo_red "red message"
echo_blue -b white "blue message, white background"
Bash echo输出带颜色和背景的文本的更多相关文章
- 让shell脚本中的echo输出带颜色
转载自:http://www.linuxidc.com/Linux/2014-12/110463.htm 让echo输出带颜色的方法有两种 1.在shell脚本中定义颜色变量,使用echo -e调用变 ...
- echo如何输出带颜色的文本
本文链接:https://blog.csdn.net/qualcent/article/details/7106483 ######################################## ...
- Bootstrap-全局样式的文本颜色和背景颜色
.text-五种颜色 文本颜色.text-info文本浅蓝颜色-提示.text-warning文本黄色-警告颜色.text-success文本绿色-成功颜色.text-primary文本深蓝色-警 ...
- 【MFC系列】MFC快速设置控件文本字体、大小、颜色、背景
以静态文本为例,分享一下怎么修改文本字体.大小.颜色.背景等参数.其他文本.控件等可参照修改. 1.修改字体.大小 这个很简单,首先在Dlg类中声明一个CFont类型的成员变量: 然后在类的初始化函数 ...
- echo -e "\033[字背景颜色;字体颜色m字符串\033[0m
格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[0m" 例如: echo -e "\033[41;36m something here \033 ...
- (转)TextView 设置背景和文本颜色的问题
在做一个项目,突然遇到如下问题 比如:在color.xml中定义了几个颜色 <color name="white">#FFFFFF</color> < ...
- (转)shell脚本输出带颜色字体
shell脚本输出带颜色字体 原文:http://blog.csdn.net/andylauren/article/details/60873400 输出特效格式控制:\033[0m 关闭所有属性 ...
- Shell~echo -e 颜色输出
echo -e 可以控制字体颜色和背景颜色输出 从一个例子开始: # echo -e "\e[1;33;41m test content \e[0m" 输出效果: . \e 转义起 ...
- php 扫描url死链接 \033[31m ANSI Linux终端输出带颜色
* 从Packagist上搜索需要的包 https://packagist.org/ * 通过composer下载依赖包 composer require guzzlehttp/guzzlecompo ...
随机推荐
- 07 C语言常量
常量的定义 常量是指固定的值,固定值在程序执行期间不会改变.这些固定值,又叫做字面量. 常量可以是任意的基本数据类型,比如整数常量.浮点常量.字符常量,或字符串字面值,也有枚举常量. 不要搞得太复杂, ...
- js日志输出还是只会console.log么,那你就out了
几乎所有的javascript开发者最常使用的日志打印调试api都是console.log(),其实还有很多的选项供我们选择,笔者下面就为大家一一介绍. 一.console.table() conso ...
- 达梦数据库_DM8配置MPP主备
为了提高MPP系统可靠性,克服由于单节点故障导致整个系统不能继续正常工作,DM 在普通的MPP系统基础上,引入主备守护机制,将MPP节点作为主库节点,增加备库作为备份节点,必要时可切换为主库代替故障节 ...
- Spring的BeanFactory是什么?
什么是BeanFactory? 提到Spring,总是让人第一时间想起IOC容器,而IOC容器的顶层核心接口就是我们的BeanFactory,如果能够理解BeanFactory的体系结构想必能让我们对 ...
- 对ACE和ATL积分
下载source code - 39.66 KB 介绍 这篇文章展示了一种结合ACE和ATL的方法.它不打算作为功能演示,而是作为一个小型的"入门"解决方案,展示实现此目标的可行方 ...
- JVM笔记五-堆区
JVM笔记五-堆区 在JVM中,堆区是重中之重.通过前面文章的学习,我们知道了,栈区是不会有垃圾回收的,所以,经常说的垃圾回收,其实就是回收的是堆区的数据.在这里,我们将会看到传说中的,新生代.老年代 ...
- Nuxt|Vue仿探探/陌陌卡片式滑动|vue仿Tinder拖拽翻牌效果
探探/Tinder是一个很火的陌生人社交App,趁着国庆假期闲暇时间倒腾了个Nuxt.js项目,项目中有个模块模仿探探滑动切换界面效果.支持左右拖拽滑动like和no like及滑动回弹效果. 一览效 ...
- rxjs入门3之项目中ajax函数封装
项目中ajax函数封装 ⽹页应⽤主要数据源有两个:⼀个是⽹页中的DOM事件,另⼀个就是通过AJAX获得的服务器资源.我们已经知道fromEvent这个操作符可以根据DOM事件产⽣Observable对 ...
- 网站搭建-云服务器ECS-镜像管理
学习笔记: 快照,系统盘可创建镜像,数据盘不可以. 实例可以直接创建镜像,包括系统盘和数据盘 复制镜像: 新购服务器,选择镜像(又买). 共享镜像: 账号ID就是UID 云市场获取镜像; 1. 创建新 ...
- 多测师讲解接口测试 _linux中搭建环境cms_高级讲师肖sir
cms后台的搭建, 我们主要是用来做接口测试和接口自动化测试: 我们搭建的流程和之前搭建多有米一样: 后期给我们一个war包就要知道搭建一个文档: 要知道环境项目怎么讲? 讲解下搭建cms环境的流 ...