欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux_Shell 设置字体 前景色 与 背景色 的几种方法

程序员文章站 2022-07-13 13:01:07
...


参考文章

1.在Linux终端输出带颜色的文字的方法

https://blog.csdn.net/slash_24/article/details/54846392

2. tput 设置字体颜色

http://*.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux



方法一 设置字符编码,  设置 文字 的前景色 与 背景色

方法二  利用tput 指令,  设置 文字 的前景色 与 背景色

总结    工作脚本中设置字体颜色脚本 示例


方法一 设置字符编码,  设置 文字 的前景色 与 背景色

示例

#!/bin/bash

orgColor=`echo -e "\033[0m" `
redColor=`echo -e "\033[31m"`


echo "an original font line"
echo "${redColor}a red font line ${orgColor}"
echo "an original font line another"


Linux_Shell 设置字体 前景色 与 背景色 的几种方法


方法二  利用tput 指令,  设置 文字 的前景色 与 背景色

#!/bin/bash

orgColor=`tput setaf 7`
redColor=`tput setaf 1`


echo "an original font line"
echo "${redColor}a red font line ${orgColor}"
echo "an original font line another"

Linux_Shell 设置字体 前景色 与 背景色 的几种方法



方法一 设置字符编码,  设置 文字 的前景色 与 背景色


一、shell下的实现方法

只要设置输出属性,就可输出带颜色的文字 ,shell中的部分属性:

      \033[0m 关闭所有属性
      \033[1m 设置高亮度
      \033[4m 下划线
      \033[5m 闪烁
      \033[7m 反显
      \033[8m 消隐
      \033[30m 至 \33[37m 设置前景色
      \033[40m 至 \33[47m 设置背景色
      \033[nA 光标上移n行 
      \033[nB 光标下移n行
      \033[nC 光标右移n行
      \033[nD 光标左移n行
      \033[y;xH设置光标位置
      \033[2J 清屏
      \033[K 清除从光标到行尾的内容
      \033[s 保存光标位置 
      \033[u 恢复光标位置
      \033[?25l 隐藏光标
      \033[?25h 显示光标


--------------------------------------------------------------------------
      各数字所代表的颜色如下:
      字背景颜色范围:40----49
      40:黑
      41:深红
      42:绿
      43:黄色
      44:蓝色
      45:紫色
      46:深绿
      47:白色

      字颜色:30----39
      30:黑
      31:红
      32:绿
      33:黄
      34:蓝色
      35:紫色
      36:深绿 
      37:白色

      使用,如:echo -e "\033[34mHello, world!" (-e作用是引导设置输出属性),
      恢复属性为默认值:echo -e  "\033[0m",
      同类的多种设置项可以组合在一起,中间用分号(;)隔开。如下:
echo -e "\033[20;1H\033[1;4;34mHello,world\033[0m"

二、C语言下的实现方法

与shell中的方法类似,如:
int color = 34;
printf("\033[20;1H\033[1;4;%dmHello, world.\033[0m", color);


三、Python下的实现方法

 color=34
 print “\033[20;1H\033[1;4;%dHello, world.\033[0m"%color




=========================================================================

=========================================================================





方法二  利用tput 指令,  设置 文字 的前景色 与 背景色


原文出自stack-overflow


Usage

Specific tput sub-commands are discussed later.

Direct

Call tput as part of a sequence of commands:

tput setaf 1; echo "this is red text"

Use ; instead of && so if tput errors the text still shows.

Shell variables

Another option is to use shell variables:

red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
echo "${red}red text ${green}green text${reset}"

tput produces character sequences that are interpreted by the terminal as having a special meaning. They will not be shown themselves. Note that they can still be saved into files or processed as input by programs other than the terminal.

Command substitution

It may be more convenient to insert tput's output directly into your echo strings using command substitution:

echo "$(tput setaf 1)Red text $(tput setab 7)and white background$(tput sgr 0)"

Example

The above command produces this on Ubuntu:

Linux_Shell 设置字体 前景色 与 背景色 的几种方法


Foreground & background colour commands

tput setab [1-7] # Set the background colour using ANSI escape
tput setaf [1-7] # Set the foreground colour using ANSI escape

Colours are as follows:

Num  Colour    #define         R G B

0    black     COLOR_BLACK     0,0,0
1    red       COLOR_RED       1,0,0
2    green     COLOR_GREEN     0,1,0
3    yellow    COLOR_YELLOW    1,1,0
4    blue      COLOR_BLUE      0,0,1
5    magenta   COLOR_MAGENTA   1,0,1
6    cyan      COLOR_CYAN      0,1,1
7    white     COLOR_WHITE     1,1,1

There are also non-ANSI versions of the colour setting functions (setb instead of setab, and setfinstead of setaf) which use different numbers, not given here.

Text mode commands

tput bold    # Select bold mode
tput dim     # Select dim (half-bright) mode
tput smul    # Enable underline mode
tput rmul    # Disable underline mode
tput rev     # Turn on reverse video mode
tput smso    # Enter standout (bold) mode
tput rmso    # Exit standout mode

Cursor movement commands

tput cup Y X # Move cursor to screen postion X,Y (top left is 0,0)
tput cuf N   # Move N characters forward (right)
tput cub N   # Move N characters back (left)
tput cuu N   # Move N lines up
tput ll      # Move to last line, first column (if no cup)
tput sc      # Save the cursor position
tput rc      # Restore the cursor position
tput lines   # Output the number of lines of the terminal
tput cols    # Output the number of columns of the terminal

Clear and insert commands

tput ech N   # Erase N characters
tput clear   # Clear screen and move the cursor to 0,0
tput el 1    # Clear to beginning of line
tput el      # Clear to end of line
tput ed      # Clear to end of screen
tput ich N   # Insert N characters (moves rest of line forward!)
tput il N    # Insert N lines

Other commands

tput sgr0    # Reset text format to the terminal's default
tput bel     # Play a bell

With compiz wobbly windows, the bel command makes the terminal wobble for a second to draw the user's attention.


Scripts

tput accepts scripts containing one command per line, which are executed in order before tputexits.

Avoid temporary files by echoing a multiline string and piping it:

echo -e "setf 7\nsetb 1" | tput -S  # set fg white and bg red

See also

  • See man 1 tput
  • See man 5 terminfo for the complete list of commands and more details on these options. (The corresponding tput command is listed in the Cap-name column of the huge table that starts at line 81.)




===============================================

=================================================





总结  工作脚本中设置字体颜色脚本 示例


代码如下:

#! /bin/bash

set -o errexit

source /etc/profile

date_pattern_old='^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$'
date_pattern='^[0-9]{4}-((0([1-9]{1}))|(1[1|2]))-(([0-2]([0-9]{1}))|(3[0|1]))$'

#参数数量
argsnum=$#

#一些默认值
curDate=`date +%Y%m%d`
partitionDate=`date -d '-1 day' +%Y-%m-%d`
fileLocDate=`date -d '-1 day' +%Y-%m-%d`

#日志存放位置
logdir=load_hdfs_data_logs

function tips() { 
	echo "Usage : load_data_into_dmp_clearlog.sh [date]"
	echo "Args :"
	echo "date"
	echo "	date use this format yyyy-MM-dd , ex : 2018-06-02"
    	echo "============================================================"
	echo "Example :"
	echo "	example1 : sh load_data_into_dmp_clearlog.sh"
	echo "	example2 : sh load_data_into_dmp_clearlog.sh 2018-06-02"
}

if [ $argsnum -eq 0 ] ; then
	echo "No argument, use default value"
elif [ $argsnum -eq 1 ] ; then
	echo "One argument, check date pattern"
	arg1=$1
	if ! [[ "$arg1" =~ $date_pattern ]] ; then
       		echo -e "\033[31m Please specify valid date in format like 2018-06-02"
       		echo -e "\033[0m"
       		tips
        	exit 1
	fi
	#echo $arg1 |tr "-" " "
	dateArr=($(echo $arg1 |tr "-" " "))
	echo "dateArr length is "${#dateArr[@]}
	partitionDate=${dateArr[0]}-${dateArr[1]}-${dateArr[2]}
	fileLocDate=${dateArr[0]}"-"${dateArr[1]}"-"${dateArr[2]}	
else 
	echo -e "\033[31m Not valid num of arguments"
	echo -e "\033[0m"
	tips
	exit 1
fi


if [ ! -d "$logdir" ]; then
    mkdir -p $logdir
fi

echo ${partitionDate}

nohup hive -hivevar p_date=${partitionDate} -hivevar f_date=${fileLocDate} -f  hdfs_add_partition_dmp_clearlog.hql  >> $logdir/load_${curDate}.log