Shell编程中的光标控制

这两天做选修课实验,有一个项目要求在屏幕中英显示信息,在网上搜了一下,发现没有太好的解决方法,唯一比较精确的是利用控制字符:

例:

[shell]
echo -ne "\33[20;40H"
[/shell]

#把光标移到20行40列

不过这样做有一个问题就是终端的行列数是不定的,没办法做到相对控制。今天在看Shell命令的时候发现了tput命令。tput -cols可以得到当前终端总列数,tput -lines可以得到总行数(不过奇怪的是man手册里没有lines这个参数,害得我开始还以为得不到行数),这样再结合echo就行了,如显示在屏幕右下角可以写成:

[shell]
#!/bin/bash
lines=`tput lines`
cols=`tput cols`
echo -ne "\33[$lines;${cols}H"
[/shell]
This entry was posted in Code. Bookmark the permalink.

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

Note: If you are replying to another commenter, click the "Reply to {NAME} ↵" button under their comment!