Shell 显示一个进度条

651阅读 0评论2010-06-03 iceway_cu
分类:LINUX

今天在cu bbs中看到一个帖子贴了几种用perl实现的进度条显示代码, perl没看过, 不过跟帖中的shell实现方法倒是不错, 以前还真没想过用shell实现, 记下来吧...

#!/bin/sh

b=''
for ((i=0;$i<=100;i+=2))
do
        printf "progress:[%-50s]%d%%\r" $b $i
        sleep 0.1
        b=#$b
done
echo



我再加一个C语言的:

#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main()
{
        char str[128] = "#";
        int i;

        for (i = 0; i < 100; i += 2) {
                fprintf(stdout, "progress:[%-50s]%d%%\r", str, i+2);
                fflush(stdout);
                strcat(str, "#");
                usleep(100000);
        }
        fprintf(
stdout, "\n");
        return 0;
}




---
原帖见:
上一篇:Shell 中的特殊变量
下一篇:Cool! Linux Shell Command