如何用 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%