ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

转:tput 命令行使用说明

2021-06-01 12:35:08  阅读:209  来源: 互联网

标签:tput bold cup color 说明 Set mode 命令行


原文链接:https://blog.csdn.net/fdipzone/article/details/9993961

什么是 tput?

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


什么是 terminfo 数据库?
UNIX 系统上的 terminfo 数据库用于定义终端和打印机的属性及功能,包括各设备(例如,终端和打印机)的行数和列数以及要发送至该设备的文本的属性。UNIX 中的几个常用程序都依赖 terminfo 数据库提供这些属性以及许多其他内容,其中包括 vi 和 emacs 编辑器以及 curses 和 man 程序。


命令行使用说明:

1.文本属性

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


输出格式控制函数:
#!/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)

<span class="hljs-keyword">case</span> <span class="hljs-string">"<span class="hljs-variable">$color</span>"</span> <span class="hljs-keyword">in</span>
	0|1|2|3|4|5|6|7)
		setcolor=$(tput setaf <span class="hljs-variable">$color</span>;) ;;
	*)
		setcolor=<span class="hljs-string">""</span> ;;
<span class="hljs-keyword">esac</span>

<span class="hljs-keyword">case</span> <span class="hljs-string">"<span class="hljs-variable">$bgcolor</span>"</span> <span class="hljs-keyword">in</span>
	0|1|2|3|4|5|6|7)
		setbgcolor=$(tput setab <span class="hljs-variable">$bgcolor</span>;) ;;
	*)
		setbgcolor=<span class="hljs-string">""</span> ;;
<span class="hljs-keyword">esac</span>

<span class="hljs-keyword">if</span> [ <span class="hljs-string">"<span class="hljs-variable">$bold</span>"</span> = <span class="hljs-string">"1"</span> ]; <span class="hljs-keyword">then</span>
	setbold=$(tput bold;)
<span class="hljs-keyword">else</span>
	setbold=<span class="hljs-string">""</span>
<span class="hljs-keyword">fi</span>

<span class="hljs-keyword">if</span> [ <span class="hljs-string">"<span class="hljs-variable">$underline</span>"</span> = <span class="hljs-string">"1"</span> ]; <span class="hljs-keyword">then</span>
	setunderline=$(tput smul;)
<span class="hljs-keyword">else</span>
	setunderline=<span class="hljs-string">""</span>
<span class="hljs-keyword">fi</span>

<span class="hljs-built_in">printf</span> <span class="hljs-string">"<span class="hljs-variable">$setcolor</span><span class="hljs-variable">$setbgcolor</span><span class="hljs-variable">$setbold</span><span class="hljs-variable">$setunderline</span><span class="hljs-variable">$str</span><span class="hljs-variable">$normal</span>\n"</span>

}

format_output "Yesterday Once More" 2 5 1 1

exit 0




2.光标属性

#!/bin/bash

tput clear # 清屏

tput sc # 保存当前光标位置

tput cup 10 13 # 将光标移动到 row col

tput civis # 光标不可见

tput cnorm # 光标可见

tput rc # 显示输出

exit 0

例子:

#!/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

标签:tput,bold,cup,color,说明,Set,mode,命令行
来源: https://www.cnblogs.com/tangshunhui/p/14836670.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有