如果要处理大量当前的终端的相关信息,比如行数,列数,光标位置和遮盖密码字段等,就要用到tput和stty这两个终端处理工具了.
获取终端的行数和列数:
tput cols
tput lines
打印当前终端名:
tput longname
将光标移到方位(100,100)处:
tput cpu 100 100
设置终端背景色:
tput bold
设置下划线的起止:
tput smul
tput rmul
删除当前光标位置到行尾的所有内容:
tput ed
下面的例子,在输入密码的时候,为了安全,不让输入的内容显示出来,将用stty来实现.脚本内容如下:
#!/bin/bash
for((i=1;i<=3;i++));do
tput setf 3
echo -n -e "Enter Password: "
stty -echo
read password
if [ $password -eq 123 ];then
echo
echo "Password is right!"
stty echo
exit 0
elif [ $i -eq 3 ];then
echo
echo "Password is wrong,BYE!"
stty echo
else
echo
echo "Password is wrong,plsase again!"
stty echo
fi
done
获取终端的行数和列数:
tput cols
tput lines
打印当前终端名:
tput longname
将光标移到方位(100,100)处:
tput cpu 100 100
设置终端背景色:
tput setb no (其中,no可以在0到7之间取值)
tput serf no (其中,no可以在0到7之间取值)
tput bold
设置下划线的起止:
tput smul
tput rmul
删除当前光标位置到行尾的所有内容:
tput ed
下面的例子,在输入密码的时候,为了安全,不让输入的内容显示出来,将用stty来实现.脚本内容如下:
#!/bin/bash
for((i=1;i<=3;i++));do
tput setf 3
echo -n -e "Enter Password: "
stty -echo
read password
if [ $password -eq 123 ];then
echo
echo "Password is right!"
stty echo
exit 0
elif [ $i -eq 3 ];then
echo
echo "Password is wrong,BYE!"
stty echo
else
echo
echo "Password is wrong,plsase again!"
stty echo
fi
done
测试结果如下图所示: