如何使进度条在 Zenity 中工作?

如何使进度条在 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%,什么也没发生。

有人能帮帮我吗?任何帮助我都会很感激。

答案1

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 希望进度能够回显给它,就像在代码示例中一样。


编辑2:建议将底部的测试更改为以下内容:

# Check return code and display an error dialog if the code is not success.
if [ "$?" != 0 ] ; then
        zenity --error \
          --text="Update canceled."
fi

这是很好的代码,但我保留了官方文档页面中的代码片段作为示例。这不是我的代码。

答案2

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

以下是我摘录的一个例子,你可以尝试一下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 论坛您可以阅读关于此脚本的讨论。如果之后您仍有疑问,请通过下面的评论提问,我会尽力为您解答。

相关内容