A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
-bash: tput: command not found #Debian apt-get install libncurses5-dbg #Ubuntu apt-get install libncurses5-dbg #Alpine apk add ncurses #Arch Linux pacman -S ncurses #Kali Linux apt-get install ncurses-bin #CentOS yum install ncurses #Fedora dnf install ncurses #OS X brew install ncurses #Raspbian apt-get install libncurses5-dbg #Docker docker run cmd.cat/tput tput
tput [-Ttype] capname [params ... ] tput [-Ttype] init tput [-Ttype] reset tput [-Ttype] longname tput -S << tput -V
tput 命令将通过 terminfo 数据库对您的终端会话进行初始化和操作。通过使用 tput,您可以更改几项终端功能,如移动或更改光标、更改文本属性,以及清除终端屏幕的特定区域
tput clear # 清除屏幕 tput sc # 记录当前光标位置 tput rc # 恢复光标到最后保存位置 tput civis # 光标不可见 tput cnorm # 光标可见 tput cup x y # 光标按设定坐标点移动
tput blink # 文本闪烁 tput bold # 文本加粗 tput el # 清除到行尾 tput smso # 启动突出模式 tput rmso # 停止突出模式 tput smul # 下划线模式 tput rmul # 取消下划线模式 tput sgr0 # 恢复默认终端 tput rev # 反相终端
更改光标的属性
在向某一设备显示数据时,很多时候您并不希望看到光标。将光标转换为不可见可以使数据滚动时的屏幕看起来更整洁。要使光标不可见,请使用 civis 选项(例如,tput civis)。在数据完全显示之后,您可以使用 cnorm 选项将光标再次转变为可见。
文本属性
更改文本的显示方式可以让用户注意到菜单中的一组词或警惕用户注意某些重要的内容。您可以通过以下方式更改文本属性:使文本加粗、在文本下方添加下划线、更改背景颜色和前景颜色,以及逆转颜色方案等。
要更改文本的颜色,请使用 setb 选项(用于设置背景颜色)和 setf 选项(用于设置前景颜色)以及在 terminfo 数据库中分配的颜色数值。通常情况下,分配的数值与颜色的对应关系如下,但是可能会因 UNIX 系统的不同而异:
0:黑色 1:蓝色 2:绿色 3:青色 4:红色 5:洋红色 6:黄色 7:白色
执行以下示例命令可以将背景颜色更改为黄色,将前景颜色更改为红色:
tput setb 6 tput setf 4
要反显当前的颜色方案,只需执行tput rev。
有时,仅为文本着色还不够,也就是说,您想要通过另一种方式引起用户的注意。可以通过两种方式达到这一目的:一是将文本设置为粗体,二是为文本添加下划线。
要将文本更改为粗体,请使用 bold 选项。要开始添加下划线,请使用 smul 选项。在完成显示带下划线的文本后,请使用 rmul 选项。
要清除当前终端的屏幕,请输入:
tput clear
要显示当前终端的列数,请输入:
tput cols
要显示 aixterm 终端的列数,请输入:
tput -T aixterm cols
要将 shell 变量 bold 设置为开始突出方式顺序并将 shell 变量 offbold 设置为结束突出方式顺序:
bold=`tput smso` offbold='tput rmso'
输入这些指令后,可能会出现以下提示符:
echo "${bold}Name: ${offbold} \c"
要设置出口值以指示当前终端是否是一个硬拷贝终端,请输入:
tput hc
要对当前终端进行初始化,请输入:
tput init
使输出的字符串有颜色,底色,加粗
提示:将下面复制写在诸如 test.sh,并添加权限 chmod u+x test.sh,之后运行 ./test.sh 查看效果):
#!/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)
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
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
################################## 运行效果如下:##################################
XYX Corp LTD.
M A I N - M E N U
1\. User Management
2\. service Management
3\. Process Management
4\. Backup
Enter your choice [1-4] 1
利用上面光标属性参数编写一个终端时钟
#!/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 # 退出时清理终端,恢复光标显示
现在为"终端时钟"添加,变换颜色和闪烁功能
#!/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 # 退出时清理终端,恢复光标显示