![在 bash 中创建进度条](https://linux22.com/image/85765/%E5%9C%A8%20bash%20%E4%B8%AD%E5%88%9B%E5%BB%BA%E8%BF%9B%E5%BA%A6%E6%9D%A1.png)
如何用 bash 创建进度条?
这是我的脚本:
#!/bin/bash
pass='number1 number12 number13 number14 number15 number16'
chk='number14'
for i in $pass ; do
if [ "$i" == "$chk" ]; then
echo ' Found ^_^'
else
echo 'loading 50%'
fi
done
我想echo 'loading 50%'
用任何东西替换来创建进度条。
答案1
答案2
你的意思是这样的吗?带有“Found”的行不管继续循环都不好,但我不知道你想在那里做什么。
#!/bin/bash
pass='number1 number12 number13 number14 number15 number16'
chk='number14'
max=0
for i in $pass ; do
let max=$max+1
done
index=0
strdone="#########################"
strtodo="-------------------------"
for i in $pass ; do
let index=$index+1
if [ "$i" == "$chk" ]; then
echo ""
echo ' Found ^_^'
else
lendone=$((index * ${#strdone} / max))
let lentodo=${#strdone}-$lendone
percent=$((index * 100 / max))
echo -ne "[#${strdone:0:$lendone}${strtodo:0:$lentodo}] $percent%\r"
fi
sleep 1
done
echo ""
输出将在最后:
[#############-------------] 50%
Found ^_^
[##########################] 100%