shell实现带颜色输出的进度条
1、基础版
#!/bin/bash b='' for ((i=0;$i<=100;i+=2)) do printf "执行进度 :[%-50s]%d%%\r" $b $i sleep 0.001 b=#$b done echo -e "\n"
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]\r" $bar $len 100 } whole=100 process=0 while [ $process -lt $whole ] do let process++ processBar $process $whole sleep 0.01 done printf "\n"
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\r" "$str" "$i" "${arr[$index]}" sleep 0.05 let i++ str+='#' done printf "\n"
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 "\033[${color};${bg}m%-s\033[0m %d %c\r" "$str" "$i" "${arry[$index]}" usleep 3000 let i=i+1 str+="#" done printf "\n"