bash - 在 bash 中存储管道函数的返回值的正确方法

标签 bash subshell zenity

我有一个函数数组,我在 bash 的循环中调用它们。每当这些函数之一返回错误时,我都会跟踪它,将该函数的名称存储在错误数组中以向用户显示。这是当前的工作脚本。

#!/bin/bash

funA()
{ 
    ls -e &> /dev/null
    return $?
}

funB()
{ 
    ls -e &> /dev/null
    return $?
}

funC()
{ 
    true
    return $?
}

taskNames=("task 1" "task 2" "task 3")
taskMessages=("performing task 1" "performing task 2" "performing task 3")
tasks=("funA" "funB" "funC")

progress=0
taskpercentage=33
errors=()

for i in ${!tasks[@]}; do

    ${tasks[i]}

    if [[ $? != 0 ]]; then
        errors+=(${taskNames[$i]})
    fi

    progress=$(expr $progress + $taskpercentage)

done

echo ${errors[@]}

exit 0

现在,我需要将循环通过管道传输到 zenity,以便向用户显示进度条。像这样的事情:

(
  progress=0
  taskpercentage=33
  errors=()

  for i in ${!tasks[@]}; do
    echo "# ${taskMessages[$i]}"

    ${funcs[i]}

    if [[ $? != 0 ]]; then
      errors+=(${taskNames[$i]})
    fi

    sleep 2

    progress=$(expr $progress + $taskpercentage)
    echo $progress
  done

  echo "# All tasks completed"
) |
zenity --progress \
       --no-cancel \
       --title="Performing all tasks" \
       --text="Performing all tasks" \
       --percentage=0 \
       --height 200 \
       --width 500

问题是,如果我将代码包装在子 shell 中,我将无法访问错误变量。有没有正确的方法来执行此操作并将更改保留到错误数组?

编辑: 我不打算只打印错误数组,而是再次通过 Zenity 将其显示给用户,类似于这样:

# Show error list
message="Some of the tasks ended with errors and could not be  completed."

if [[ ${#errors[@]} > 0 ]]; then
    zenity --list --height 500 --width 700 --title="title" \
    --text="$message" \
    --hide-header --column "Tasks with errors" "${errors[@]}"  
fi

最佳答案

如果您需要做的只是打印错误消息,则可以将其放在单独的文件描述符或单独的文件中。据我所知,使用临时文件的最易读的方式:

tempname=$(mktemp)     # Create a zero-length file
(
    # ... your subshell

    if [[ ${#errors[@]} -gt 0 ]]; then       # save all $errors entries to the
        printf '%s\n' "${errors[@]}" > "$tempname"     # file called $tempname
    fi
) | zenity # ... your progress code

# After the zenity call, report errors
if [[ -s $tempname ]]; then   # -s: size > 0
    message="Some of the tasks ended with errors and could not be  completed."  
    zenity --list --height 500 --width 700 --title="title" --text="$message" \
        --hide-header --column "Tasks with errors" < "$tempname"
fi        # Provide the saved errors to the dialog ^^^^^^^^^^^^^
rm -f "$tempname"    # always remove, since mktemp creates the file.

编辑:

  1. 所有error可以使用printf打印条目,并用换行符分隔。每this answer (another option)。

  2. [[ -s $tempname ]]检查文件是否名为 $tempname存在并且大小大于零。如果是这样,则意味着存在一些错误,我们将其保存到该文件中。

  3. 根据 Zenity list-dialog documentation ,

    Data can be provided to the dialog through standard input. Each entry must be separated by a newline character.

    所以,zenity --list ... < "$tempname"提供以前 ${errors[@]} 中的项目,并保存到临时文件、列表对话框中。

替代方案:您还可以使用 2>&3 等方式通过管道移动信息,但我对我的 bash hackery 没有足够的信心,无法立即尝试这样做。 :) 这是a related question和一个detailed walkthrough of bash redirection .

关于bash - 在 bash 中存储管道函数的返回值的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41127569/

相关文章:

linux - 如果 bash 脚本收到 ctrl z,它如何杀死自己

linux - 从 jar 中提取 txt 文件

Bash Sed 查找并替换为特殊字符

bash - bash 变量何时导出到子 shell 和/或可由脚本访问?

linux - 错误 : ld. 所以:无法预加载对象 LD_PRELOAD:忽略

bash - 如何将多个命令通过管道传输到 shell 中的单个命令? (嘘,庆典,...)

bash - bash 变量何时导出到子 shell 和/或可由脚本访问?

bash - 如何在 bash 中 grep 直到循环中的字符串?

bash - 在 bash 中显示 zenity 对话框时运行命令

linux - Linux Mint 中的“RUN”不起作用