首页 > 解决方案 > 检测进程替换的退出状态

问题描述

我目前正在使用 bash 4.1,并且正在使用一个函数在存储库文件上执行 SVN cat。之后,它遍历每一行以执行一些转换(主要是连接等)。如果所述文件不存在,则脚本应停止并显示错误消息。脚本如下:

function getFile {
    svnCat=`svn cat (file) 2>&1` 
    if [[ -n $(echo "$svnCat" | grep "W160013") ]]; then # W160013 is the returned value by SVN stderr case a file doesn't exist
        echo "File doesn't exist" >&2
        exit 1
    else
        echo "$svnCat" | while read -r; do
            #Do your job
        done
    fi
}

function processFile{
    while read -r do
        #do stuff
    done < <(getFile)

    #do even more stuff
}

但是,在文件不存在的情况下,会打印一次错误消息,但脚本会继续执行。有没有办法检测到 while 循环失败并且应该完全停止脚本?

无法使用 set -e 选项,因为我需要删除在此过程中创建的一些文件。

更新:我尝试添加 || 完成命令后退出如下:

function processFile{
    while read -r do
        #do stuff
    done || exit 1 < <(getFile)

但是,脚本正在等待用户输出,当我按下回车键时,它会执行 while 循环中的内容

标签: bashunix

解决方案


跟踪进程替换的退出状态很棘手,并且需要非常现代的 bash 版本(在我的脑海中,我想说 4.3 或更高版本)。在此之前,$!after<(getFile)不会被正确填充,因此wait会失败(或者,更糟糕的是,引用之前启动的子进程)。

#!/usr/bin/env bash

### If you *don't* want any transforms at this stage, eliminate getFile entirely
### ...and just use < <(svn cat "$1") in processFile; you can/should rely on svn cat itself
### ...to have a nonzero exit status in the event of *any* failure; if it fails to do so,
### ...file a bug upstream.
getFile() {
    local content
    content=$(svn cat "$1") || exit  # pass through exit status of failed svn cat
    while read -r line; do
      echo "Generating a transformed version of $line"
    done <<<"$content"
}

processFile() {
    local getFileFd getFilePid line

    # start a new process running getFile; record its pid to use to check exit status later
    exec {getFileFd}< <(getFile "$1"); getFilePid=$!

    # actual loop over received content
    while IFS= read -r line; do
      echo "Retrieved line $line from process $getFilePid"
    done <&"$getFileFd"

    # close the FIFO from the subprocess
    exec {getFileFd}<&-

    # then use wait to wait for it to exit and collect its exit status
    if wait "$getFilePid"; then
      echo "OK: getFile reports success" >&2
    else
      getFileRetval=$?
      echo "ERROR: getFile returned exit status $getFileRetval" >&2
    fi
}

推荐阅读