使用tput和dialog增强脚本用户体验
立即解锁
发布时间: 2025-09-09 01:53:49 阅读量: 11 订阅数: 23 AIGC 


Linux命令行进阶之旅
### 使用 tput 和 dialog 增强脚本用户体验
#### 1. tput 的使用
在 Linux 系统中,terminfo 数据库定义了许多不同的终端类型和终端功能名称(capnames)。不过在实际使用中,我们在 Linux 系统上遇到的终端类型通常只支持相对较少的一部分 capnames。
##### 1.1 读取终端属性
tput 可以输出一些终端属性的值到标准输出,以下是一些常见的 capname 及其描述:
| Capname | 描述 |
| ---- | ---- |
| longname | 终端类型的全名 |
| lines | 终端的行数 |
| cols | 终端的列数 |
| colors | 可用的颜色数量 |
其中,`lines` 和 `cols` 的值是动态的,会随着终端窗口大小的变化而更新。我们可以使用以下别名来查看当前终端窗口的大小:
```bash
alias term_size=`echo "Rows=$(tput lines) Cols=$(tput cols)"`
```
定义并执行这个别名后,会显示当前终端的大小。当我们改变终端窗口大小后再次执行该别名,显示的值会更新。
我们还可以利用 SIGWINCH 信号,该信号会在终端窗口大小改变时发送。以下是一个示例脚本:
```bash
#!/bin/bash
# term_size2 - Dynamically display terminal window size
redraw() {
clear
echo "Width = $(tput cols) Height = $(tput lines)"
}
trap redraw WINCH
redraw
while true; do
:
done
```
在这个脚本中,我们启动了一个空的无限循环,并设置了一个信号处理函数来捕获 SIGWINCH 信号。每次终端窗口大小改变时,`redraw` 函数会被触发,显示新的终端大小。按 Ctrl - c 可以退出该脚本。
##### 1.2 控制光标
以下 capnames 可以输出控制代码字符串,用于控制终端中的光标:
| Capname | 描述 |
| ---- | ---- |
| sc | 保存光标位置 |
| rc | 恢复光标位置 |
| home | 将光标移动到左上角 (0,0) |
| cup <row> <col> | 将光标移动到指定的行和列 |
| cud1 | 将光标向下移动 1 行 |
| cuu1 | 将光标向上移动 1 行 |
| civis | 设置光标为不可见 |
| cnorm | 设置光标为正常状态 |
我们可以修改之前的脚本,使用光标定位并将窗口尺寸信息显示在终端中央:
```bash
#!/bin/bash
# term_size3 - Dynamically display terminal window size
# with text centering
redraw() {
local str width height length
width=$(tput cols)
height=$(tput lines)
str="Width = $width Height = $height"
length=${#str}
clear
tput cup $((height / 2)) $(((width / 2) - (length / 2)))
echo "$str"
}
trap redraw WINCH
redraw
while true; do
:
done
```
这个脚本同样设置了 SIGWINCH 信号的捕获和无限循环,但 `redraw` 函数更复杂,需要每次窗口大小改变时计算终端窗口的中心位置。
##### 1.3 文本效果
类似于光标操作的 capnames,以下 capnames 可以输出控制代码字符串,影响终端显示文本字符的方式:
| Capname | 描述 |
| ---- | ---- |
| bold | 开始显示粗体文本 |
| smul | 开始显示下划线文本 |
| rmul | 结束显示下划线文本 |
| rev | 开始显示反显文本 |
| blink | 开始显示闪烁文本 |
| invis | 开始显示不可见文本 |
| smso | 开始显示“突出”模式 |
| rmso | 结束显示“突出”模式 |
| sgr0 | 关闭所有属性 |
| setaf <value> | 设置前景色 |
| setab <value> | 设置背景色 |
有些属性(如下划线和突出模式)有开启和关闭的 capname,而有些只有开启的 capname,此时可以使用 `sgr0` 恢复文本显示为“正常”状态。以下是一个演示常见文本效果的简单脚本:
```bash
#!/bin/bash
# tput_characters - Test various character attributes
clear
echo "tput character test"
echo "==================="
echo
tput bold; echo "This text has the bold attribute."; tput sgr0
tput smul; echo "This text is underlined (smul)."; tput rmul
# Most terminal emulators do not support blinking text (though xterm
# does) because blinking text is considered to be in bad taste ;-)
tput blink; echo "This text is blinking (blink)."; tput sgr0
tput rev; echo "This text has the reverse attribute"; tput sgr0
# Standout mode is reverse on many terminals, bold on others.
tput smso; echo "This text is in standout mode (smso)."; tput rmso
tput sgr0
echo
```
##### 1.4 文本颜色
大多数终端支持 8 种前景文本颜色和 8 种背景颜色(有些支持多达 256 种)。使用 `setaf` 和 `setab` 功能可以设置前景色和背景色。颜色的具体显示可能难以预测,因为许多桌面管理器会对终端窗口应用“系统颜色”。以下是颜色值对应的颜色:
| 值 | 颜色 |
| ---- | ---- |
| 0 | 黑色 |
| 1 | 红色 |
| 2 | 绿色 |
| 3 | 黄色 |
| 4 | 蓝色 |
| 5 | 品红色 |
| 6 | 青色 |
| 7 | 白色 |
| 8 | 未使用 |
| 9 | 恢复为默认颜色 |
以下脚本使用 `setaf` 和 `setab` 功能显示可用的前景/背景颜色组合:
```bash
#!/bin/bash
# tput_colors - Demonstrate color combinations.
for fg_color in {0..7}; do
set_foreground=$(tput setaf $fg_color)
for bg_color in {0..7}; do
set_background=$(tput setab $bg_color)
echo -n $set_background$set_foreground
printf ' F:%s B:%s ' $fg_color $bg_color
done
echo $(tput sgr0)
done
```
##### 1.5 清除屏幕
以下 capnames 可以选择性地清除终端显示的部分内容:
| Capname | 描述 |
| ---- | ---- |
| smcup | 保存屏幕内容 |
| rmcup | 恢复屏幕内容 |
| el | 从光标位置清除到行尾 |
| el1 | 从光标位置清除到行首 |
| ed | 从光标位置清除到屏幕末尾 |
| clear | 清除整个屏幕并将光标移到左上角 |
以下是一个使用这些终端功能的脚本,它包含一个菜单和一个单独的输出区域,用于显示系统信息:
```bash
#!/bin/bash
# tput_menu: a menu driven system information program
BG_BLUE="$(tput setab 4)"
BG_BLACK="$(tput setab 0)"
FG_GREEN="$(tput setaf 2)"
FG_WHITE="$(tput setaf 7)"
# Save screen
tput smcup
# Display menu until selection == 0
while [[ $REPLY != 0 ]]; do
echo -n ${BG_BLUE}${FG_WHITE}
clear
cat <<- _EOF_
Please Select:
1. Display Hostname and Uptime
2. Display Disk Space
3. Display Home Space Utilization
0. Quit
_EOF_
read -p "Enter selection [0-3] > " selection
# Clear area beneath menu
tput cup 10 0
echo -n ${BG_BLACK}${FG_GREEN}
tput ed
tput cup 11 0
# Act on selection
case $selection in
1) echo "Hostname: $HOSTNAME"
uptime
;;
2) df -h
;;
3) if [[
```
0
0
复制全文
相关推荐









