shell脚本实现进度条

时间:2023-03-09 19:28:26
shell脚本实现进度条

使用shell脚本编写进度条

可已加入到shell脚本当中

主要作用:好看 美观 没毛用

(一)

普通进度条:

#!/bin/bash
b=''
for ((i=0;$i<=20;i++))
do
let jinshu=$i*5
printf "[%-20s]%d%%\r" $b $jinshu
sleep 0.1
b=#$b
done
echo

shell脚本实现进度条

倍数为5 (jinshu=$i* )$i乘以几倍数就是几

调整倍数后需调整  (“#?”代表要调整的参数)

((i=0;i<=#?;i++))
printf "[%-#?s]%d%%\r" $b $jinshu

(二)

#!/bin/bash

i=0
str=""
label=('|' '/' '-' '\\')
index=0
while [ $i -le 20 ]
do
let index=i%4
let jinshu=$i*5
printf "\e[47m\e[31m[%-20s]\e[0m\e[47;32m[%c]\e[1;0m\e[47;35m[%-3d%%]\e[1;0m\r" $str ${label[$index]} $jinshu
let i++
str+="#"
sleep 0.1
done
echo

shell脚本实现进度条

(三)

#!/bin/bash
# echo "准备中..."
i=0
str=""
arr=("|" "/" "-" "\\")
while [ $i -le 20 ]
do
let index=i%4
let indexcolor=i%8
let color=30+indexcolor
let NUmbER=$i*5
printf "\e[0;$color;1m[%-20s][%d%%]%c\r" "$str" "$NUmbER" "${arr[$index]}"
sleep 0.1
let i++
str+='+'
done
printf "\n"
# printf "正在执行...稍候!"

shell脚本实现进度条

进度条可以用到执行比较危险操作的shell脚本中

提示用户 如果不想执行 可在进度条出按ctrl+c结束脚本

因为 在进度条跑的过程中是不会执行 下面的代码的

也可以设置执行的任务和进度条代码一起执行

见博客 shell多进程

https://www.cnblogs.com/LuckWJL/p/9674347.html