2016-07-10 29 views
0

我想调用一个shell函数,并且在此函数处理的同时,应该显示一个zenity进度对话框。 但是,我希望将该函数的echo'ed字符串存储在变量中以供进一步处理,以及该函数的返回码。Zenity - 进程返回字符串和返回码(POSIX shell)

而这一切都在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函数的输出?

回答

0

您可以创建一个子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 

我用它来创建一个小的 “GUI” 包装到sha256sum,像这样:

(
    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