编辑清楚:
假设我有以下脚本(假设 pv 和 curl 已经安装):
(目前在 ubuntu 下运行,但我计划使其兼容 POSIX,以便它可以在更多 Linux 发行版上运行)
#!/bin/bash
sudo apt install vlc
mkdir -p ~/.steam/compatibilitytools.d
PROTONVERSIONNUMBER=$(curl -v --silent https://api.github.com/repos/popsUlfr/Proton/releases 2>&1 | grep "tag_name" | head -n 1 | cut -f4,4 -d"\"")
REPLACING=$(curl -v --silent https://api.github.com/repos/popsUlfr/Proton/releases 2>&1 | grep "target_commitish" | head -n 1 | cut -f4,4 -d"\"" | sed "s/[^_]\+/\L\u&/g")
PROTONVERSION=${REPLACING/_G/-6_G}
PROTONNAME=$PROTONVERSION"_"${PROTONVERSIONNUMBER##*-}
wget https://github.com/popsUlfr/Proton/releases/download/$PROTONVERSIONNUMBER/$PROTONNAME.tar.xz
pv $PROTONNAME.tar.xz | tar xp -J -C ~/.steam/compatibilitytools.d
rm $PROTONNAME.tar.xz
我得到三个进度条,这些进度条对我来说看起来非常漂亮:
他们的准确性之类的,我不知道称我为怪人
问题
如何利用这三个独立进度条的力量来形成一个连续进度条,该进度条尊重底层“真实”进度条的当前进度条“速度”?
答案1
下面是一段示例代码,演示了如何让多个“工作”部分全部更新同一个进度表。 Whiptail 仪表附加到脚本的文件描述符 3,以便可以在脚本期间的任何时刻更新它。 (当脚本结束或显式关闭 FD 3 时,仪表自动退出。)
#!/bin/bash
#
pid=
tmpd=
tidyUp()
{
# Clean up when we're done
exec 3>&-
[[ -n "$tmpd" ]] && rm -rf "$tmpd"
}
trap 'ss=$?; tidyUp; exit $ss' 1 2 15
updateGauge()
{
local percent="$1" message="$2"
printf "XXX\n%d\n%s\nXXX\n" $percent "$message" >&3
}
# Create the FIFO for communicating with the whiptail gauge
tmpd=$(mktemp --tmpdir --directory "wt.XXXXXXXXXX")
mkfifo "$tmpd/fifo"
# Start up the whiptail gauge and associate FD 3 with its status
whiptail --title 'Progress meter' --gauge 'Starting examples' 6 50 0 <"$tmpd/fifo" &
exec 3>"$tmpd/fifo"
# Real code starts here
percent=0
for example in 1 2 3
do
updateGauge $percent "Getting example $example"
sleep 3 # wget something
percent=$((percent + 20))
done
for another in 4 5
do
updateGauge $percent "Doing work for another example $another"
sleep 2 # do some work
percent=$((percent + 20))
done
# Done
tidyUp
exit 0