【问题标题】:Zenity --progress return string and return code (POSIX shell)Zenity --progress 返回字符串和返回码(POSIX shell)
【发布时间】:2016-07-10 12:25:00
【问题描述】:

我正在尝试调用一个 shell 函数,当这个函数处理时,应该会显示一个 zenity 进度对话框。 但是,我希望将该函数的回显字符串存储在一个变量中以供进一步处理,以及该函数的返回码。

所有这些都在 POSIX shell 中。

我目前的做法是这样的:

output="$( compress "${input}" |  \
    zenity --progress \
    --pulsate \
    --title="Compressing files" \
    --text="Scanning mail logs..." \
    --percentage=0 \
)";

if [ "$?" != "0" ]; then
    echo "${output}"
    exit 1
fi

显示进度对话框,但最后$output 为空。

知道如何获取compress 函数的输出吗?

【问题讨论】:

    标签: shell posix zenity


    【解决方案1】:

    zenity 不这样做。对于进度对话框,它返回给环境的只是一个退出代码。

    在源码中可以看到:

    zenity 打印到您的shell 的唯一文本 是额外按钮文本。它只是更新 GUI 并丢弃进度消息的文本,例如,zenity_progress_update_time_remaining

    【讨论】:

      【解决方案2】:

      您可以创建一个子shell 并在其中运行命令。唯一需要注意的是,在进度对话框完成后执行的命令不允许写入标准输出。否则会出现 I/O 错误。

      在你的情况下,这看起来像这样:

      (
          output="$(compress "${input}")"
      
          if [ "$?" != "0" ]; then
              #echo "${output}" <- this would result in an I/O error because the pipe is closed
              # write to somewhere else, maybe standard error like so:
              echo "${output}" >&2
              exit 1
          fi
      ) | \
          zenity --progress \
          --pulsate \
          --title="Compressing files" \
          --text="Scanning mail logs..." \
          --percentage=0
      

      我用它为 sha256sum 创建了一个小的“GUI”包装器,如下所示:

      (
        HASH=$(sha256sum "$1")
        # send EOF to end the zenity progress dialog
        exec 1>&-
        zenity --title="sha256sum" --info --text="$HASH" --no-wrap
      ) | zenity --progress  --title="sha256sum" --pulsate --auto-close --no-cancel
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-13
        • 2017-08-06
        • 1970-01-01
        • 1970-01-01
        • 2014-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多