如何在Zenity中使用进度条?

我正在编写一个简单的基本bash脚本(以#! /bin/bash开头,文件格式为.sh ),我正在努力使进度条工作:

 #!/bin/bash echo "You are running in LXDE mode. Answer 'yes' or 'no' on the following question to continue (or not) in LXDE mode." zenity --question --text='Do you want to continue in LXDE mode?' --ok-label=Yes --cancel-label=No echo "Please enter your username and password to continue because the following command needs root privileges." zenity --password --username echo "Please enter today's date:" zenity --calendar --text="Please enter today's date:" echo "Please enter your name:" zenity --entry --text='Please enter your name on the text entry below:' echo "Analyzing data..." zenity --info --text='Now begin analyzing data. If it takes more than 40 seconds, click on "Cancel".' zenity --progress --title='Analyzing data...' --pulsate 

我试图让它从0%变为100%,但没有任何反应。 它被停留在0%。 我还试图通过使用--pulsate选项使其脉动,仍为0%无所事事。

谁能帮帮我吗? 任何帮助,将不胜感激。

Zenity文档有一个小代码片段,应该完全符合您的要求。

 #!/bin/sh ( echo "10" ; sleep 1 echo "# Updating mail logs" ; sleep 1 echo "20" ; sleep 1 echo "# Resetting cron jobs" ; sleep 1 echo "50" ; sleep 1 echo "This line will just be ignored" ; sleep 1 echo "75" ; sleep 1 echo "# Rebooting system" ; sleep 1 echo "100" ; sleep 1 ) | zenity --progress \ --title="Update System Logs" \ --text="Scanning mail logs..." \ --percentage=0 if [ "$?" = -1 ] ; then zenity --error \ --text="Update canceled." fi 

首先尝试复制代码并运行它并确认它按预期工作,然后修改以在适当的位置添加代码。

如果您的进度条卡在零,请确保绕过可能挂起的脚本的任何部分,并让您认为它实际上正在工作!

编辑:如下面的答案中所述,它不起作用的原因是因为zenity期望将进程回显给它,就像在代码示例中一样。

zenity用于显示进度条的方式是通过|bash脚本捕获echo命令 (管道)重定向命令(符号)。

以下是我可以尝试从Ubuntu论坛中提取的示例:

 #!/bin/bash # Force Zenity Status message box to always be on top. ( # ================================================================= echo "# Running First Task." ; sleep 2 # Command for first task goes on this line. # ================================================================= echo "25" echo "# Running Second Task." ; sleep 2 # Command for second task goes on this line. # ================================================================= echo "50" echo "# Running Third Task." ; sleep 2 # Command for third task goes on this line. # ================================================================= echo "75" echo "# Running Fourth Task." ; sleep 2 # Command for fourth task goes on this line. # ================================================================= echo "99" echo "# Running Fifth Task." ; sleep 2 # Command for fifth task goes on this line. # ================================================================= echo "# All finished." ; sleep 2 echo "100" ) | zenity --progress \ --title="Progress Status" \ --text="First Task." \ --percentage=0 \ --auto-close \ --auto-kill (( $? != 0 )) && zenity --error --text="Error in zenity command." exit 0 

如果您点击Ubuntu论坛的链接,您可以阅读有关此脚本的讨论。 如果在那之后您还有问题,请通过下面的评论询问,我会尽力为您解答。