shell脚本学习总结04--终端信息的获取和设置

tput

tput 命令将通过 terminfo 数据库对您的终端会话进行初始化和操作。通过使用 tput,您可以更改几项终端功能,如移动或更改光标、更改文本属性,以及清除终端屏幕的特定区域。

文本属性

 

复制代码
    tput Color Capabilities:  
      
    tput setab [0-7] – Set a background color using ANSI escape  
    tput setb [0-7] – Set a background color  
    tput setaf [0-7] – Set a foreground color using ANSI escape  
    tput setf [0-7] – Set a foreground color  
      
    Color Code for tput:  
      
    0 – Black  
    1 – Red  
    2 – Green  
    3 – Yellow  
    4 – Blue  
    5 – Magenta  
    6 – Cyan  
    7 – White  
      
    tput Text Mode Capabilities:  
      
    tput bold – Set bold mode  
    tput dim – turn on half-bright mode  
    tput smul – begin underline mode  
    tput rmul – exit underline mode  
    tput rev – Turn on reverse mode  
    tput smso – Enter standout mode (bold on rxvt)  
    tput rmso – Exit standout mode  
    tput sgr0 – Turn off all attributes  
复制代码

 例一,使输出的字符串有颜色,底色,加粗

 

复制代码
#!/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
复制代码

运行结果:

 例2,输出格式控制函数

复制代码
#!/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)
           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" 3 1 1 1

    exit 0
复制代码

运行结果:

光标属性

复制代码
tput clear # 清屏  
  
tput sc # 保存当前光标位置  
  
tput cup 10 13 # 将光标移动到 row col  
  
tput civis # 光标不可见  
  
tput cnorm # 光标可见  
  
tput rc # 显示输出  
复制代码

例一

复制代码
    #!/bin/bash  
    # clear the screen  
    tput clear  
    # Move cursor to screen location X,Y (top left is 0,0)  
    tput cup 3 15  
    # Set a foreground colour using ANSI escape  
    tput setaf 3  
    echo "XYX Corp LTD."  
    tput sgr0  
    tput cup 5 17  
    # Set reverse video mode  
    tput rev  
    echo "M A I N - M E N U"  
    tput sgr0  
    tput cup 7 15  
    echo "1. User Management"  
    tput cup 8 15  
    echo "2. Service Management"  
    tput cup 9 15  
    echo "3. Process Management"  
    tput cup 10 15  
    echo "4. Backup"  
    # Set bold mode  
    tput bold  
    tput cup 12 15  
    read -p "Enter your choice [1-4] " choice  
    tput clear  
    tput sgr0  
    tput rc   
    exit 0  
复制代码

运行结果:

例2:

复制代码
#/bin/bash
echo -n  Count:
tput sc
count=0
while true;do
if [ $count -lt 40 ];then
let let count++
sleep 1
tput rc
tput ed
echo -n $count;
else exit o
fi
done
复制代码

运行结果:

1.获取终端的行数和列数

# tput cols
98
# tput lines
26

2. 移动光标

复制代码
[root@IHS ~]# tput cup 10 0









[root@IHS ~]# 
复制代码

stty

使输入的密码不在屏幕上显示

#/bin/bash
echo -e "Enter password:"
stty -echo
read passwd
stty echo

 

posted @   头痛不头痛  阅读(765)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示
主题色彩