shell实现带颜色输出的进度条

时间:2023-11-30 11:17:44

1、基础版

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

2、普通版

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

3、进阶版

#!/bin/bash
i=;
str=""
arr=("|" "/" "-" "\\")
while [ $i -le ]
do
let index=i%
let indexcolor=i%
let color=+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=
str=""
arry=("\\" "|" "/" "-")
while [ $i -le ]
do
let index=i%
if [ $i -le ]
then
let color=
let bg=
elif [ $i -le ]
then
let color=
let bg=
elif [ $i -le ]
then
let color=
let bg=
else
let color=
let bg=
#根据功能需求还可以更改
fi
printf "\033[${color};${bg}m%-s\033[0m %d %c\r" "$str" "$i" "${arry[$index]}"
usleep
let i=i+
str+="#"
done
printf "\n"