当前位置:网站首页>The shell updates the terminal output information in place
The shell updates the terminal output information in place
2022-08-08 04:29:00 【Curious binary】
Foreword
Sometimes we need to update the same line of output information instead of outputting a new line, such as a progress display.Two implementation methods:
Output the backspace character \b, clear the input character on the line
Output the carriage return character \r, and then overwrite the output character
For the first implementation, how many characters have been outputted on the line, it needs to be output exactlyThe same amount of \b to prevent some terminal implementations from eating the carriage return on the previous line as well.You can use the return value of printf() to get the number of characters printed.
For the second implementation, the number of new characters output must be at least as many as already output to completely overwrite the previous output.
I prefer the second implementation, as long as the number of characters output in each line is the same (the number of output characters of the parameter specified by printf()), the implementation is more concise and maintainable.
This article gives implementation examples of C language and Shell respectively.
C language implementation
#include #include int main(void){int i;for (i = 0; i < 100; i++) {printf("\rprogress: %3d %%", i);fflush(stdout);sleep(1);}printf("\n");return 0;}It is important to note that the terminal device is a line buffer device, that is, unless a newline character \n is output or the buffer is full, printf() will not have any output; so here you need to use fflush(stdout) to clear the standard outputbuffer to immediately output the buffer contents.
Shell implementation
#!/bin/bashfor i in {1..100}doprintf "\rprocess %3d %%" $isleep 1done# print a new lineechoThe printf command line program can be used in scripts.
边栏推荐
- sessionStorage在不同页签中的数据是否共享问题及解决思路
- 全网唯一OpenCyphal/UAVCAN教程(11)用candump和gawk工具写一个Cyphal协议解析小工具
- vulnhub-DC-5靶机渗透记录
- 实现Express中间件原理
- 报错:[Intervention] Unable to preventDefault inside passive event listener due to target ...
- 内修昇思MindSpore AI框架,外重行业汇聚,华为大模型的不平凡之路
- Redis persistence mechanism, master-slave, sentry, cluster parsing cluster solution
- Monitoring tool Prometheus and project summary, 220805,,
- MySQL从入门到入土【20W字收藏篇】
- NetCore使用Dapper查询数据
猜你喜欢

32. 你知道Redis的字符串是怎么实现的吗?

NetCore使用Dapper查询数据

leetcode-同构字符串判断

ToDesk企业版上新 | 十大新功能,让企业远控更安全、更便捷、更流畅

fail-fast 和 fail-safe 快速学习

拒绝“内卷”跃迁软件测试最大门槛,我是如何从月薪8K到15K的?
![报错:[Intervention] Unable to preventDefault inside passive event listener due to target ...](/img/57/44e6bc7a330f075f79eb428475d89c.png)
报错:[Intervention] Unable to preventDefault inside passive event listener due to target ...

一文带你彻底了解synchronized 和 Lock

【多任务CTR】阿里ESMM:Entire Space Multi-Task Model: An Effective Approach for Estimating Post-Click Conve

New retail project and offline warehouse core interview,, 220807,,
随机推荐
L3-006 迎风一刀斩
Machine Learning Notes: Learning Rate Warmup
How to avoid bugs as much as possible
NetCore uses Dapper to query data
NetCore使用Dapper查询数据
【多任务CTR】阿里ESMM:Entire Space Multi-Task Model: An Effective Approach for Estimating Post-Click Conve
风控策略必学|这种用决策树来挖掘规则的方法
KMP和EXKMP(Z函数)
一文带你彻底了解synchronized 和 Lock
强网杯 2019-随便注 (堆叠注入)
MindFusion.WPF Pack 2022.R1
【冷启动】快手《POSO: Personalized Cold Start Modules for Large-scale Recommender Systems》
MySql入门教程
一行代码统计文本中指定字符串出现的次数
MySQL从入门到入土【20W字收藏篇】
项目分析(嵌入式产品中的硬件设计、生产)
leetcode 70.爬楼梯 动态规划
Exercise equipment responsive pbootcms template class web site
Redis持久化机制、主从、哨兵、cluster集群方案解析
Mini Program Optimization Practice