1-9采集终端信息

1.9采集终端信息

写脚本时,常常要获取当前终端的设置信息。

1.9.1 预备知识

tput和stty是两款终端处理工具。

1.9.2 实战演练

1.tput命令
  • 获取终端的行数和列数

tput cols
tput lines
  • 打印当前终端名

tput longname
  • 将光标移动到(100,100)处

tput cup 100 100
  • 设置终端背景色

tput setb n		#n位于0-7
  • 设置终端前景色

tput setf n		#n位于0-7
  • 设置文本粗体

tput bold
  • 设置下划线的起止

tput smul
tput rmul
  • 删除从当前光标位置到行尾的所有内容

tput ed
2.stty命令

输入密码时,脚本不应该显示输入内容。

stty命令的选项-echo禁止将输出发送到终端,而选项echo则允许发送输出

#!/bin/bash
#Filename: password.sh
echo -e "Enter the passwords:" #启用反斜杠转义

#在读取密码前禁止回显
stty -echo
read password #使用read命令将密码读取到变量password里
#重新允许回显
stty echo
echo
echo Password read