shell脚本之获取终端信息

 如果要处理大量当前的终端的相关信息,比如行数,列数,光标位置和遮盖密码字段等,就要用到tput和stty这两个终端处理工具了.
   获取终端的行数和列数:
   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

测试结果如下图所示:



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值