• 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 "
    "
  • 相关阅读:
    智力题:砝码称重问题
    Java:Comparator接口
    机器学习中比较重要的几个概念
    NLP:Gensim库之word2vec
    Java遍历Map对象的四种方式
    给数据库减负刻不容缓:多级缓存设计
    CentOS6.10安装redis5.0
    Linux下安装whl文件
    Linux下安装Gensim
    Linux下安装jieba
  • 原文地址:https://www.cnblogs.com/qianjingchen/p/9604224.html
Copyright © 2020-2023  润新知