如何向shell脚本添加进度条?

编程入门 行业动态 更新时间:2024-10-08 08:27:41
本文介绍了如何向shell脚本添加进度条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在 bash 或 *NIX 中的任何其他 shell 中编写脚本时,在运行需要几秒钟以上的命令时,需要一个进度条.

When scripting in bash or any other shell in *NIX, while running a command that will take more than a few seconds, a progress bar is needed.

比如复制一个大文件,打开一个大的tar文件.

For example, copying a big file, opening a big tar file.

您建议通过哪些方式向 shell 脚本添加进度条?

What ways do you recommend to add progress bars to shell scripts?

推荐答案

您可以通过覆盖一行来实现.使用 回到行首,无需将 写入终端.

You can implement this by overwriting a line. Use to go back to the beginning of the line without writing to the terminal.

在完成行进后写 .

Write when you're done to advance the line.

使用 echo -ne 来:

  • 不打印 和
  • 识别转义序列,如 .
  • not print and
  • to recognize escape sequences like .
  • 这是一个演示:

    echo -ne '##### (33%)' sleep 1 echo -ne '############# (66%)' sleep 1 echo -ne '####################### (100%)' echo -ne ' '

    在下面的评论中,puk 提到如果你从长行开始然后想写一个短行,这个失败":在这种情况下,你需要覆盖长行的长度(例如,用空格).

    In a comment below, puk mentions this "fails" if you start with a long line and then want to write a short line: In this case, you'll need to overwrite the length of the long line (e.g., with spaces).

    更多推荐

    如何向shell脚本添加进度条?

    本文发布于:2023-11-26 09:28:58,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1633564.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:脚本   进度条   shell

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!