您的位置:首页 > 运维架构 > Shell

Android系统shell中的clear命令实现

2017-06-19 15:23 302 查看
之前一直不太清楚,当我们在shell命令行输入很多命令,会在屏幕上输出一些信息,为什么一执行clear这个命令以后,所有的信息就没了呢?
现在终于搞明白了,找到了clear命令的源代码clear.c
源码如下:
#include <stdio.h>

int clear_main(int argc, char **argv) {
/* This prints the clear screen and move cursor to top-left corner control
* characters for VT100 terminals. This means it will not work on
* non-VT100 compliant terminals, namely Windows' cmd.exe, but should
* work on anything unix-y. */
fputs("\x1b[2J\x1b[H", stdout);
return 0;
}

震惊了!!!就两行代码!!!这里面稀奇古怪的字符串重定向到stdout(标准输出)是什么东西呢?其实是一串VT100的控制码,那这一串代码什么东西呢?
"\x1b[2J",//清除整个屏幕,行属性变成单宽单高,光标位置不变

"\x1b[H",//光标移动

更加详细的命令可以参考以下博文: http://blog.sina.com.cn/s/blog_7347cd380100upwj.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: