首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

tput 入门(2)

tput 入门(2)

将 shell 脚本提升到下一级别现在您已经了解了从命令行中执行 tput 的基本知识,下面我们将重点讲述如何将您学到的知识连同其他一些功能用到 shell 脚本中。首先,tput 提供了以下一些附加功能:提取终端信息(如设备,列数和行数)和清除屏幕上的数据。               
要确定当前的列数(即,您在目标设备上可以使用的宽度),请使用 cols 选项。要查找行数(即行当前的高度),请使用 lines 选项。               
您可以使用几种方法清除数据,具体取决于需要的结果。要清除从当前光标位置到行尾的数据,可以使用 tput el。要清除从当前光标位置到设备末尾的数据,可以使用 tput ed。如果您想要清除整个设备,请使用 tput clear。               
将其全部放到一个脚本中下面的代码创建了一个基本菜单。此脚本介绍了如何在 tput 中使用本文中介绍的多个选项增强您的代码。                       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
trap `get_window_size` WINCH                    # trap when a user has resized the window

_UNDERLINE_ON=`tput smul`                       # turn on underline
_UNDERLINE_OFF=`tput rmul`                     # turn off underline

get_window_size() {
  _WINDOW_X=`tput lines`
  _WINDOW_Y=`tput cols`

  _FULL_SPACES=`echo ""|awk `
  {
    _SPACES = `${_WINDOW_Y}`
    while (_SPACES-- > 0) printf (" ")
  }'`
  _FULL_UNDERLINE=`echo "${_UNDERLINE_ON}${_FULL_SPACES}${_UNDERLINE_OFF}"`

  unset _FULL_SPACES
  show_menu

  return 0
}

set_color() {
  tput clear
  PS3="Enter Selection[1-9]:"
  select _COLOR in "Black" "Blue" "Green" "Cyan" "Red" "Magenta" "Yellow" "White" "Exit"
  do
    case ${REPLY} in
       [1-8])  _X=`expr ${REPLY} - 1`;;
           9)  break;;
           *)  echo "Invalid Color"; continue;;
    esac

    if [[ ${1} = "b" ]]
    then
      tput setb ${_X}
    else
      tput setf ${_X}
    fi
  done
}

show_menu() {
  while [[ -z ${_ANS} ]]
  do
    tput civis
    tput clear

        cat <<- EOF
                Window Size: ${_WINDOW_X} / ${_WINDOW_Y}

                Select => ${_UNDERLINE_ON}     ${_UNDERLINE_OFF}

                ${_FULL_UNDERLINE}
                B) Background Text Color
                F) Foreground Text Color

                X) Exit
        EOF

    tput rc
    tput smul
    tput cnorm

    read _ANS
    tput rmul

    case ${_ANS} in
      [Bb])  set_color "b";;
      [Ff])  set_color "f";;
      [Xx])  tput clear; exit;;
         *)
             echo -e "Invalid Selection: ${_ANS}\c"
             sleep 2
             ;;
    esac
    unset _ANS
  done
}

tput sgr0
tput civis
tput clear
tput cup 3 10
tput sc
tput cup 0 0

[[ -n ${_ANS} ]] && unset _ANS
get_window_size

exit 0




下面我们分析一下 shell 脚本。
设置解释脚本的方式。在本例中,要使用的 shell 为 Bash。为 WINCH 信号设置一个陷阱,同时指定 get_window_size 函数作为捕获到的信号的触发器。在设置了陷阱之后,定义两个变量以便稍后在脚本中键入时使用。                       
1
2
3
4
5
#!/bin/bash
trap `get_window_size` WINCH                    # trap when a user has resized the window

_UNDERLINE_ON=`tput smul`                       # turn on underline
_UNDERLINE_OFF=`tput rmul`                      # turn off underline




创建一个名为 get_widow_size 的函数用来确定行数和列数。此外,定义 _FULL_UNDERLINE 变量,设备的宽度(带有下划线)。                       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
get_window_size() {
  _WINDOW_X=`tput lines`
  _WINDOW_Y=`tput cols`

  _FULL_SPACES=`echo ""|awk `
  {
    _SPACES =`${_WINDOW_Y}`
    while (_SPACES-- > 0) printf (" ")
  }'`
  _FULL_UNDERLINE=`echo "${_UNDERLINE_ON}${_FULL_SPACES}${_UNDERLINE_OFF}"`

  unset _FULL_SPACES
  show_menu

  return 0
}




创建一个名为 set_color 的函数来允许用户测试背景和前景文本颜色。                       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
set_color() {
  tput clear
  PS3="Enter Selection[1-9]:"
  select _COLOR in "Black" "Blue" "Green" "Cyan" "Red" "Magenta" "Yellow" "White" "Exit"
  do
    case ${REPLY} in
       [1-8])  _X=`expr ${REPLY} - 1`;;
           9)  break;;
           *)  echo "Invalid Color"; continue;;
    esac

    if [[ ${1} = "b" ]]
    then
      tput setb ${_X}
    else
      tput setf ${_X}
    fi
  done
}




创建一个名为 show_menu 的函数,通过此函数来演示设备的大小。此函数中演示的内容还包括:将光标转变为不可见,清除屏幕,打印文本,以及返回到保存的光标位置。                       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
show_menu() {
  while [[ -z ${_ANS} ]]
  do
    tput civis
    tput clear

        cat <<- EOF
                Window Size: ${_WINDOW_X} / ${_WINDOW_Y}

                Select => ${_UNDERLINE_ON}     ${_UNDERLINE_OFF}

                ${_FULL_UNDERLINE}
                B) Background Text Color
                F) Foreground Text Color

                X) Exit
        EOF

    tput rc
    tput smul
    tput cnorm

    read _ANS
    tput rmul

    case ${_ANS} in
      [Bb])  set_color "b";;
      [Ff])  set_color "f";;
      [Xx])  tput clear; exit;;
         *)
             echo -e "Invalid Selection: ${_ANS}\c"
             sleep 2
             ;;
    esac
    unset _ANS
  done
}




接下来,设置一些基本的光标属性。首先,可以使用 sgr0 清除所有属性。光标将转换为不可见,并且屏幕将被清除。不可见的光标现在移动到 (3,10),此位置将被保存,然后光标将移动到 (0,0)(左上角)。                       
1
2
3
4
5
6
tput sgr0
tput civis
tput clear
tput cup 3 10
tput sc
tput cup 0 0




最后,调用 get_window_size 函数获取窗口大小,进而调用 function show 菜单。                       
1
2
3
4
[[ -n ${_ANS} ]] && unset _ANS
get_window_size

exit 0

返回列表