Linux Command tput 终端操作

简介: Linux Command tput 终端操作

Linux Command tput 终端操作

tags: 编辑器

文章目录

Linux Command tput 终端操作

1. 简介

2. 什么是 terminfo 数据库

3. 参数

4. 属性

4.1 光标属性

4.2 移动光标

4.3 移动光标并显示信息

4.4 更改光标的属性

4/5 文本属性

5. 实例

5.1 变量格式

5.2 使字符串有颜色,底色,加粗

5.3 格式传参输出

5.4 光标定位输出

5.5 输出艺术字体

5.6 时钟倒计时

1. 简介

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


与 UNIX 中的大多数命令一样,tput 命令既可以用在 shell 命令行中也可以用在 shell 脚本中。为让您更好地理解 tput,本文首先从命令行讲起,然后紧接着讲述 shell 脚本示例。

2. 什么是 terminfo 数据库

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


与 UNIX 中的大多数命令一样,tput 命令既可以用在 shell 命令行中也可以用在 shell 脚本中。为让您更好地理解 tput,本文首先从命令行讲起,然后紧接着讲述 shell 脚本示例。

3. 参数

(1)字符串输出参数设置 
  bel       警铃 
  blink     闪烁模式 
  bold      粗体 
  civis     隐藏光标 
  clear     清屏 
  cnorm     不隐藏光标 
  cup       移动光标到屏幕位置(x,y) 
  el        清除到行尾 
  ell       清除到行首 
  smso      启动突出模式 
  rmso      停止突出模式 
  smul      开始下划线模式 
  rmul      结束下划线模式 
  sc        保存当前光标位置 
  rc        恢复光标到最后保存位置 
  sgr0      正常屏幕 
  rev       逆转视图 
(2)数字输出参数设置 
  cols      列数目 
  ittab     设置宽度 
  lines     屏幕行数 
(3)布尔输出参数设置 
  chts      光标不可见 
  hs        具有状态行  
(4)景色
   setaf ColorNumber## 设置前景色
   setab ColorNumber ##设置背景色

4. 属性

4.1 光标属性

在 UNIX shell 脚本中或在命令行中,移动光标或更改光标属性可能是非常有用的。有些情况下,您可能需要输入敏感信息(如密码),或在屏幕上两个不同的区域输入信息。在此类情况下,使用 tput 可能会对您有所帮助。

tput clear # 清屏
tput sc # 保存当前光标位置
tput cup 10 13 # 将光标移动到 x y
tput civis # 光标不可见
tput cnorm # 光标可见
tput rc # 显示输出,返回光标位置
exit 0

4.2 移动光标

使用 tput 可以方便地实现在各设备上移动光标的位置。通过在 tput 中使用 cup 选项,或光标位置,您可以在设备的各行和各列中将光标移动到任意 X 或 Y 坐标。设备左上角的坐标为 (0,0)。


要在设备上将光标移动到第 5 列 (X) 的第 1 行 (Y),只需执行 tput cup 5 1。另一个示例是 tput cup 23 45,此命令将使光标移动到第 23 列上的第 45 行。

4.3 移动光标并显示信息

另一种有用的光标定位技巧是移动光标,执行用于显示信息的命令,然后返回到前一光标位置:

(tput sc ; tput cup 23 45 ; echo “Input from tput/echo at 23/45” ; tput rc)

下面我们分析一下 subshell 命令:

tput sc

必须首先保存当前的光标位置。要保存当前的光标位置,请包括 sc 选项或“save cursor position”。

tput cup 23 45

在保存了光标位置后,光标坐标将移动到 (23,45)。

echo “Input from tput/echo at 23/45”

将信息显示到 stdout 中。

tput rc

在显示了这些信息之后,光标必须返回到使用 tput sc 保存的原始位置。要使光标返回到其上次保存的位置,请包括 rc 选项或“restore cursor position”。


注意:由于本文首先详细介绍了通过命令行执行 tput,因此您可能会觉得在自己的 subshell中执行命令要比单独执行每条命令然后在每条命令执行之前显示提示更简洁。

4.4 更改光标的属性

在向某一设备显示数据时,很多时候您并不希望看到光标。将光标转换为不可见可以使数据滚动时的屏幕看起来更整洁。要使光标不可见,请使用 civis 选项(例如,tput civis)。在数据完全显示之后,您可以使用 tput cnorm 选项将光标再次转变为可见。

4/5 文本属性

更改文本的显示方式可以让用户注意到菜单中的一组词或警惕用户注意某些重要的内容。您可以通过以下方式更改文本属性:使文本加粗、在文本下方添加下划线、更改背景颜色和前景颜色,以及逆转颜色方案等。


要更改文本的颜色,请使用 setb 选项(用于设置背景颜色)和 setf 选项(用于设置前景颜色)以及在 terminfo 数据库中分配的颜色数值。通常情况下,分配的数值与颜色的对应关系如下,但是可能会因 UNIX 系统的不同而异:


0:黑色

1:蓝色

2:绿色

3:青色

4:红色

5:洋红色

6:黄色

7:白色

执行以下示例命令可以将背景颜色更改为黄色,将前景颜色更改为红色:

tput setb 6 tput setf 4

1832b220aa754cd18c504acc7686a560.png

要反显当前的颜色方案,只需执行tput rev。


有时,仅为文本着色还不够,也就是说,您想要通过另一种方式引起用户的注意。可以通过两种方式达到这一目的:一是将文本设置为粗体,二是为文本添加下划线。


要将文本更改为粗体,请使用 bold 选项。要开始添加下划线,请使用 smul 选项。在完成显示带下划线的文本后,请使用 rmul 选项。

5. 实例

5.1 变量格式

#加粗
bold=$(tput bold)
#下划线
underline=$(tput sgr 0 1)
#重置规则
reset=$(tput sgr0)
#红色
red=$(tput setaf 1)
#绿色
green=$(tput setaf 2)

5.2 使字符串有颜色,底色,加粗

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

1832b220aa754cd18c504acc7686a560.png

5.3 格式传参输出

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

1832b220aa754cd18c504acc7686a560.png

5.4 光标定位输出

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

1832b220aa754cd18c504acc7686a560.png

5.5 输出艺术字体

root@test1:~/shell/tput# apt install figlet
root@test1:~/shell/tput# wget http://www.figlet.org/fonts/roman.flf
--2021-11-18 15:44:37--  http://www.figlet.org/fonts/roman.flf
Resolving www.figlet.org (www.figlet.org)... 188.226.162.120
Connecting to www.figlet.org (www.figlet.org)|188.226.162.120|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12948 (13K) [text/plain]
Saving to: ‘roman.flf’
roman.flf                                                      100%[====================================================================================================================================================>]  12.64K  --.-KB/s    in 0s      
2021-11-18 15:44:38 (70.6 MB/s) - ‘roman.flf’ saved [12948/12948]
root@test1:~/shell/tput# echo; tput setaf 1; figlet -c  Happy; tput setaf 2; figlet -c  Holidays; tput sgr0;
                     _   _                         
                    | | | | __ _ _ __  _ __  _   _ 
                    | |_| |/ _` | '_ \| '_ \| | | |
                    |  _  | (_| | |_) | |_) | |_| |
                    |_| |_|\__,_| .__/| .__/ \__, |
                                |_|   |_|    |___/ 
                 _   _       _ _     _                 
                | | | | ___ | (_) __| | __ _ _   _ ___ 
                | |_| |/ _ \| | |/ _` |/ _` | | | / __|
                |  _  | (_) | | | (_| | (_| | |_| \__ \
                |_| |_|\___/|_|_|\__,_|\__,_|\__, |___/
                                             |___/     


root@test1:~/shell/tput# echo; tput setaf 1; figlet -c -f roman Happy; tput setaf 2; figlet -c -f roman Holidays; tput sgr0;
           ooooo   ooooo                                             
           `888'   `888'                                             
            888     888   .oooo.   oo.ooooo.  oo.ooooo.  oooo    ooo 
            888ooooo888  `P  )88b   888' `88b  888' `88b  `88.  .8'  
            888     888   .oP"888   888   888  888   888   `88..8'   
            888     888  d8(  888   888   888  888   888    `888'    
           o888o   o888o `Y888""8o  888bod8P'  888bod8P'     .8'     
                                    888        888       .o..P'      
                                   o888o      o888o      `Y8P'       
 ooooo   ooooo           oooo   o8o        .o8                                 
 `888'   `888'           `888   `"'       "888                                 
  888     888   .ooooo.   888  oooo   .oooo888   .oooo.   oooo    ooo  .oooo.o 
  888ooooo888  d88' `88b  888  `888  d88' `888  `P  )88b   `88.  .8'  d88(  "8 
  888     888  888   888  888   888  888   888   .oP"888    `88..8'   `"Y88b.  
  888     888  888   888  888   888  888   888  d8(  888     `888'    o.  )88b 
 o888o   o888o `Y8bod8P' o888o o888o `Y8bod88P" `Y888""8o     .8'     8""888P' 
                                                          .o..P'               
                                                          `Y8P'                

5.6 时钟倒计时

  #!/bin/bash
    # tclock - Display a clock in a terminal
    BG_BLUE="$(tput setab 4)"
    FG_BLACK="$(tput setaf 0)"
    FG_WHITE="$(tput setaf 7)"
    terminal_size() { # Calculate the size of the terminal
      terminal_cols="$(tput cols)"
      terminal_rows="$(tput lines)"
    }
    banner_size() {
      # Because there are different versions of banner, we need to
      # calculate the size of our banner's output
      banner_cols=0
      banner_rows=0
      while read; do
        [[ ${#REPLY} -gt $banner_cols ]] && banner_cols=${#REPLY}
        ((++banner_rows))
      done < <(banner "12:34 PM")
    }
    display_clock() {
      # Since we are putting the clock in the center of the terminal,
      # we need to read each line of banner's output and place it in the
      # right spot.
      local row=$clock_row
      while read; do
        tput cup $row $clock_col
        echo -n "$REPLY"
        ((++row))
      done < <(banner "$(date +'%I:%M %p')")
    }
    # Set a trap to restore terminal on Ctrl-c (exit).
    # Reset character attributes, make cursor visible, and restore
    # previous screen contents (if possible).
    trap 'tput sgr0; tput cnorm; tput rmcup || clear; exit 0' SIGINT
    # Save screen contents and make cursor invisible
    tput smcup; tput civis
    # Calculate sizes and positions
    terminal_size
    banner_size
    clock_row=$(((terminal_rows - banner_rows) / 2))
    clock_col=$(((terminal_cols - banner_cols) / 2))
    progress_row=$((clock_row + banner_rows + 1))
    progress_col=$(((terminal_cols - 60) / 2))
    # In case the terminal cannot paint the screen with a background
    # color (tmux has this problem), create a screen-size string of 
    # spaces so we can paint the screen the hard way.
    blank_screen=
    for ((i=0; i < (terminal_cols * terminal_rows); ++i)); do
      blank_screen="${blank_screen} "
    done
    # Set the foreground and background colors and go!
    echo -n ${BG_BLUE}${FG_WHITE}
    while true; do
      # Set the background and draw the clock
      if tput bce; then # Paint the screen the easy way if bce is supported
        clear
      else # Do it the hard way
        tput home
        echo -n "$blank_screen"
      fi
      tput cup $clock_row $clock_col
      display_clock
      # Draw a black progress bar then fill it in white
      tput cup $progress_row $progress_col
      echo -n ${FG_BLACK}
      echo -n "###########################################################"
      tput cup $progress_row $progress_col
      echo -n ${FG_WHITE}
      # Advance the progress bar every second until a minute is used up
      for ((i = $(date +%S);i < 60; ++i)); do
        echo -n "#"
        sleep 1
      done
    done

b41705914ab149ed906428aa9032a946.gif

参考:

相关文章
|
4月前
|
Linux 网络安全 iOS开发
SecureCRT & SecureFX 9.6.3 for macOS, Linux, Windows - 跨平台的多协议终端仿真和文件传输
SecureCRT & SecureFX 9.6.3 for macOS, Linux, Windows - 跨平台的多协议终端仿真和文件传输
1119 4
SecureCRT & SecureFX 9.6.3 for macOS, Linux, Windows - 跨平台的多协议终端仿真和文件传输
|
6月前
|
JavaScript Linux 网络安全
Termux安卓终端美化与开发实战:从下载到插件优化,小白也能玩转Linux
Termux是一款安卓平台上的开源终端模拟器,支持apt包管理、SSH连接及Python/Node.js/C++开发环境搭建,被誉为“手机上的Linux系统”。其特点包括零ROOT权限、跨平台开发和强大扩展性。本文详细介绍其安装准备、基础与高级环境配置、必备插件推荐、常见问题解决方法以及延伸学习资源,帮助用户充分利用Termux进行开发与学习。适用于Android 7+设备,原创内容转载请注明来源。
1137 77
|
11月前
|
运维 Java Linux
Linux 下命令后台运行秘籍:无惧终端断开的魔法
本文详细介绍了在 Linux 系统下使命令不受终端断开影响、持续在后台运行的多种方法及其原理。包括使用 `nohup`、`setsid`、括号括起来、作业调度和 `screen` 等技巧,帮助读者提高工作效率,确保任务不被意外中断。
312 0
Linux 下命令后台运行秘籍:无惧终端断开的魔法
|
网络协议 Linux 网络安全
入职必会-开发环境搭建39-Linux常用操作-Linux防火墙操作
在CentOS 7中,新引入了firewalld服务(防火墙),取代了CentOS 6之前的iptables服务(防火墙)。
129 5
入职必会-开发环境搭建39-Linux常用操作-Linux防火墙操作
|
存储 运维 Linux
Linux磁盘精准缩容:操作详解与技巧
在Linux系统管理中,有效的磁盘空间优化对于维护系统性能至关重要。本文将深入探讨如何在Linux环境下安全地进行磁盘缩容,帮助你合理调整存储资源,确保系统高效运行。跟随本篇的步骤,一起优化你的Linux系统磁盘空间!
Linux磁盘精准缩容:操作详解与技巧
|
Shell Linux API
C语言在linux环境下执行终端命令
本文介绍了在Linux环境下使用C语言执行终端命令的方法。首先,文章描述了`system()`函数,其可以直接执行shell命令并返回结果。接着介绍了更强大的`popen()`函数,它允许程序与命令行命令交互,并详细说明了如何使用此函数及其配套的`pclose()`函数。此外,还讲解了`fork()`和`exec`系列函数,前者创建新进程,后者替换当前进程执行文件。最后,对比了`system()`与`exec`系列函数的区别,并针对不同场景推荐了合适的函数选择。
|
存储 安全 Linux
|
NoSQL Linux 开发工具
Linux终端革命:掌握这些命令,让工作速度飞跃提升!
本文介绍了Linux命令行操作效率提升的关键技巧,包括光标移动快捷键、Vim编辑器的高效使用、快速切换目录、跨服务器文件拷贝等。通过掌握`Ctrl + a`、`Ctrl + e`等快捷键可加快命令编辑;Vim的`:set nu`、`:20`等命令能提升文本编辑速度;`cd -`命令可在最近访问过的目录间快速切换;利用`nc`或`python -m SimpleHTTPServer`可实现在无密码权限时的文件传输。这些技巧帮助用户提高工作效率,简化日常工作流程。
311 1
|
Linux 数据安全/隐私保护
【Deepin 20 系统】Linux系统在开机时未进入系统前进入命令行界面(终端)
如何在Deepin 20系统启动时进入命令行界面(终端),通过在GRUB界面中编辑内核启动参数来引导系统进入多用户文本模式(运行级别3)。
793 1
|
Ubuntu Linux 测试技术
Linux终端玩转bastet俄罗斯方块小游戏
【7月更文挑战第14天】Linux终端玩转bastet俄罗斯方块小游戏
212 7