zoukankan      html  css  js  c++  java
  • shell实现带颜色输出的进度条

    1、基础版

    #!/bin/bash
    b=''
    for ((i=0;$i<=100;i+=2))
    do
            printf "执行进度 :[%-50s]%d%%
    " $b $i
            sleep 0.001
            b=#$b  
    done
    echo -e "
    "

    2、普通版

    #!/bin/bash
    processBar()
    {
        now=$1
        all=$2
        percent=`awk BEGIN'{printf "%f", ('$now'/'$all')}'`
        len=`awk BEGIN'{printf "%d", (100*'$percent')}'`
        bar='>'
        for((i=0;i<len-1;i++))
        do
            bar="#"$bar
        done
        printf "[%-100s][%03d/%03d]
    " $bar $len 100
    }
    
    whole=100
    process=0
    while [ $process -lt $whole ] 
    do
        let process++
        processBar $process $whole
        sleep 0.01
    done
    printf "
    "

    3、进阶版

    #!/bin/bash
    i=0;
    str=""
    arr=("|" "/" "-" "\")
    while [ $i -le 100 ]
    do
      let index=i%4
      let indexcolor=i%8
      let color=30+indexcolor
      printf "e[0;$color;1m[%-100s][%d%%]%c
    " "$str" "$i" "${arr[$index]}"
      sleep 0.05
      let i++
     str+='#'
    done
    printf "
    "

    4、高级版

    #!/bin/bash
    i=0
    str=""
    arry=("\" "|" "/" "-")
    while [ $i -le 100 ]
    do
      let index=i%4
      if  [ $i -le 20 ]
        then
        let color=44
        let bg=34
      elif [ $i -le 45 ]
        then
        let color=43
         let bg=33
      elif [ $i -le 75 ]
         then
         let color=41
         let bg=31
      else
         let color=42
         let bg=32
    #根据功能需求还可以更改
      fi
      printf "33[${color};${bg}m%-s33[0m %d %c
    " "$str" "$i" "${arry[$index]}"
      usleep 3000
      let i=i+1
      str+="#"
    done
    printf "
    "
  • 相关阅读:
    c#大圣之路笔记——c# 页面加载数据过长等待显示框
    Python(五)
    Python(四)
    Python(三)
    python(二)数据类型与变量
    初识Python
    Linux初识(九)
    Linux初识(八)正则表达式
    Linux基础初识(七)
    Linux基础初识(六)
  • 原文地址:https://www.cnblogs.com/qianjingchen/p/9604224.html
Copyright © 2011-2022 走看看