如何打印出相同的路线,覆盖previous行? [英] How to print out to the same line, overriding previous line?

查看:165
本文介绍了如何打印出相同的路线,覆盖previous行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我看到在终端打印结果到标准输出,但在同一行的一些命令。例如wget的打印箭头象下面几行:

Sometimes I see some commands in the terminal that print results to stdout but in the same line. For example wget prints an arrow like below lines:

0[=>        ]100%
0[  =>      ]100%
0[    =>    ]100%
0[      =>  ]100%
0[        =>]100%

但它打印出来的同一行,所以它看起来像箭头移动。我如何使用bash或sh实现我的计划是一回事吗?我是否需要另外其他工具?

but it is printed out to the same line so it looks like the arrow is moving. How can I achieve the same thing in my programs using bash or sh? Do I need another other tools?

更新:

我知道我提到的wget,附带在默认情况下在Linux中,基于GNU Unix系统......有没有过在BSD系统工作的一般方法? (如OSX) - >确定,如果我使用bash,而不是SH然后它的工作原理:)

I know I mentioned wget, which comes by default in linux, GNU based unices ... Is there a general approach that works on BSDs too? (like OSX) -> OK, If I use bash instead of sh then it works :)

推荐答案

使用特殊字符 \\ r 。它返回到行开头而不进入下一个。

Use the special character \r. It returns to the beginning of the line without going to the next one.

for i in {1..10} ; do
    echo -n '['
    for ((j=0; j<i; j++)) ; do echo -n ' '; done
    echo -n '=>'
    for ((j=i; j<10; j++)) ; do echo -n ' '; done
    echo -n "] $i"0% $'\r'
    sleep 1
done

这篇关于如何打印出相同的路线,覆盖previous行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆