我正在尝试在 bash 脚本中实现动态进度条,这是我们在安装新软件包时看到的那种。为了做到这一点,随机任务将调用进度条脚本作为后台任务,并为其提供一些整数值。
第一个脚本使用管道来提供第二个脚本。
#!/bin/bash
# randomtask
pbar_x=0 # percentage of progress
pbar_xmax=100
while [[ $pbar_x != $pbar_xmax ]]; do
echo "$pbar_x"
sleep 1
done | ./progressbar &
# do things
(( pbar_x++ ))
# when task is done
(( pbar_x = pbar_xmax ))
因此,第二个脚本需要不断地接收整数并打印出来。
#!/bin/bash
# progressbar
while [ 1 ]; do
read x
echo "progress: $x%"
done
但是在这里,第二个脚本在更新时不会收到这些值。我做错什么了 ?