当前位置:网站首页>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.
边栏推荐
- fail-fast 和 fail-safe 快速学习
- 类似Bugfree的9大在线缺陷管理软件
- CARLA 笔记(05)— Actors and blueprints(创建和修改 Blueprint、生成 Spawning、使用 Handling、销毁 Destruction)
- XDR technology
- ES6对象字面量的新功能
- vulnhub-DC-5靶机渗透记录
- VSCode打开 C(嵌入式) 工程的一些记录
- The type of block in the database buffer cache
- 32. Do you know how Redis strings are implemented?
- Qt 日志模块的个性化使用
猜你喜欢

After being unemployed for 6 months at home, I bought a house with full payment through outsourcing: the industries you look down on are often very profitable

leetcode: 874. 模拟行走机器人

向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!

NetCore使用Dapper查询数据

数据在内存如何分布的?

L3-006 迎风一刀斩

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

【模板引擎】velocity

The research project of the Institute of Metal Research of the Chinese Academy of Sciences has been certified by Huawei, helping to develop a new paradigm in materials science!

MySql入门教程
随机推荐
Some excellent blog recommendations for Qt event learning reference
Machine Learning Notes: Learning Rate Warmup
【多任务CTR】阿里ESMM:Entire Space Multi-Task Model: An Effective Approach for Estimating Post-Click Conve
package package
内修昇思MindSpore AI框架,外重行业汇聚,华为大模型的不平凡之路
【保研面试】英文问题
由联合体union引出的大小端问题
Open3D 基于颜色的ICP配准
6G时代新用户面设计和关键技术
Redis持久化机制、主从、哨兵、cluster集群方案解析
C语言 扫雷
数据在内存如何分布的?
vulnhub-DC-3靶机渗透记录
项目分析(嵌入式产品中的硬件设计、生产)
The research project of the Institute of Metal Research of the Chinese Academy of Sciences has been certified by Huawei, helping to develop a new paradigm in materials science!
vulnhub-DC-5靶机渗透记录
响应式pbootcms模板健身器械类网站
手把手教你手撕SPF生成树(OSPF区域内防环)
KMP和EXKMP(Z函数)
Week 4 Step by step building multi-layer neural network and application (1 & 2)