(转)tput命令行使用说明

发布时间 2023-11-24 11:25:43作者: Cong0ks

tput 命令行使用说明

https://blog.csdn.net/fdipzone/article/details/9993961

1.tput简介
tput可以更改终端功能,如移动或更改光标,更改文本属性,清除终端屏幕的特定区域等。

2.命令行使用说明
2.1光标属性
tput clear #清除屏幕
tput sc #记录当前光标位置
tput rc #恢复光标到最后保存位置
tput civis #光标不可见
tput cnorm #光标可见
tput cup x y #光标按设定坐标点移动

2.2文本属性
tput blink #文本闪烁
tput bold #文本加粗
tput el #清除到行尾
tput smso #启动突出模式
tput rmso #停止突出模式
tput smul #下划线模式
tput rmul #取消下划线模式
tput sgr0 #恢复默认终端
tput rev #反相终端

2.3 颜色

2.3.1 文本颜色

tput setb 颜色代号
tput setf 颜色代号
颜色代号
0:黑色
1:蓝色
2:绿色
3:青色
4:红色
5:洋红色
6:黄色
7:白色

2.3.2 背景颜色

1. 前景色:
数字颜色数字颜色
0 黑色 4 蓝色
1 红色 5 紫色
2 绿色 6 青色
3 黄(或棕)色 7 白(或灰)色

2. 背景色:
数字颜色数字颜色
0 黑色 4 青色
1 红色 5 蓝色
2 绿色 6 青色
3 黄(或棕)色 7 白(或灰)色

例子:黑底绿字echo "33[40;32m"

利用上面参数编写一个终端时钟

#!/bin/bash

for ((i=0;i<10;i++))
do
        tput sc; tput civis                     #记录光标位置,及隐藏光标
        echo -ne $(date +'%Y-%m-%d %H:%M:%S')   #显示时间
        sleep 1
        tput rc                                 #恢复光标到记录位置
done

tput el; tput cnorm                             #退出时清理终端,恢复光标显示

4.现在为"终端时钟"添加,变换颜色和闪烁功能

#!/bin/bash

for ((i=0;i<8;i++))
do
        tput sc; tput civis                     #记录光标位置,及隐藏光标
        tput blink; tput setf $i                #文本闪烁,更改文本颜色
        echo -ne $(date +'%Y-%m-%d %H:%M:%S')   #显示时间
        sleep 1
        tput rc                                 #恢复光标到记录位置
done

tput el; tput cnorm                             #退出时清理终端,恢复光标显示

5.使输出的字符串有颜色,底色,加粗

#!/bin/bash

printf $(tput setaf 2; tput bold)'color show\n\n'$(tput sgr0)

for((i=0; i<=7; i++)); do
    echo $(tput setaf $i)"show me the money"$(tput sgr0)
done

printf '\n'$(tput setaf 2; tput setab 0; tput bold)'background color show'$(tput sgr0)'\n\n'

for((i=0,j=7; i<=7; i++,j--)); do
    echo $(tput setaf $i; tput setab $j; tput bold)"show me the money"$(tput sgr0)
done

exit 0

6.输出格式控制函数

#!/bin/bash

# $1 str       print string
# $2 color     0-7 设置颜色
# $3 bgcolor   0-7 设置背景颜色
# $4 bold      0-1 设置粗体
# $5 underline 0-1 设置下划线

function format_output(){
    str=$1
    color=$2
    bgcolor=$3
    bold=$4
    underline=$5
    normal=$(tput sgr0)

    case "$color" in
        0|1|2|3|4|5|6|7)
            setcolor=$(tput setaf $color;) ;;
        *)
            setcolor="" ;;
    esac

    case "$bgcolor" in
        0|1|2|3|4|5|6|7)
            setbgcolor=$(tput setab $bgcolor;) ;;
        *)
            setbgcolor="" ;;
    esac

    if [ "$bold" = "1" ]; then
        setbold=$(tput bold;)
    else
        setbold=""
    fi

    if [ "$underline" = "1" ]; then
        setunderline=$(tput smul;)
    else
        setunderline=""
    fi

    printf "$setcolor$setbgcolor$setbold$setunderline$str$normal\n"
}

format_output "Yesterday Once More" 2 5 1 1

7.总结
tput sc #记录光标位置
tput cup x y #移动光标至x列y行
tput rc #返回光标位置
tput civis #隐藏光标
tput cnorm #示光标
tput setaf ColorNumber #设置前景色
tput setab ColorNumber #设置背景色

 

转自:版权声明:本文为CSDN博主「东城绝神」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_37814112/article/details/103159605