我已经dialog
安装了,我想要一个不错的进度对话框,如下所示:
+-------[ title ]--------+
| |
| +-[ console output ]-+ |
| | output line11 ^ |
| | output line12 | |
| | output line13 | |
| | output line14 # |
| | output line15 | |
| | output line16 v |
| +--------------------+ |
| #########80%#####::::: |
+------------------------+
Run Code Online (Sandbox Code Playgroud)
因此,例如,当在发行版中升级 5 个软件包时,它会显示进度(此处,4 个软件包升级了 5 个,即 80%),但会显示执行命令的详细输出。这可能吗?
我怀疑是这样,但我无法通过--tailboxbg
和获得有效的解决方案--gauge
。
是的,你可以这样做--gauge
:
#!/bin/bash
declare PACKAGES=("/etc/crontab" "/etc/dmtab" "/etc/fstab" "/etc/inittab" "/etc/mtab")
NUM_PACKAGES=${#PACKAGES[*]} # no. of packages to update (#packages in the array $PACKAGES)
step=$((100/$NUM_PACKAGES)) # progress bar step
cur_file_idx=0
counter=0
DEST=${HOME}
(
# infinite while loop
while :
do
cat <<EOF
XXX
$counter
$counter% upgraded
$COMMAND
XXX
EOF
COMMAND="cp ${PACKAGES[$cur_file_idx]} $DEST &>/dev/null" # sets/updates command to exec.
[[ $NUM_PACKAGES -lt $cur_file_idx ]] && $COMMAND # executes command
(( cur_file_idx+=1 )) # increase counter
(( counter+=step ))
[ $counter -gt 100 ] && break # break when reach the 100% (or greater
# since Bash only does integer arithmetic)
sleep 10 # delay it a specified amount of time i.e. 1 sec
done
) |
dialog --title "File upgrade" --gauge "Please wait..." 10 70 0
Run Code Online (Sandbox Code Playgroud)
这段代码在行动:
注意。此代码实际上将这五个文件复制/etc/
到您的$HOME
文件夹中。