Sock Em
Sock Em

Reputation: 29

Detecting exit status on process substitution

I'm currently using bash 4.1 and I'm using a function to perform a SVN cat on a repository file. After that, it iterates over each line to perform some transformations (mostly concatenations and such). If said file does not exist, the script should stop with an error message. The script is as follows:

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
}

However, in situations where a file does not exist, the error message is printed once but the script keeps executing. Is there a way to detect that the while looped failed and should stop the script completely?

Can't use set -e option since I require to delete some files that were created in the process.

Update: I've tried to add || exit after the done command as follows:

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

However, the script is waiting for user output and when I press enter, it executes the content in the while loop

Upvotes: 2

Views: 321

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295443

Tracking exit status from a process substitution is tricky, and requires a very modern version of bash (off the top of my head, I want to say 4.3 or newer). Prior to that, the $! after the <(getFile) will not be correctly populated, and so the wait will fail (or, worse, refer to a previously-started subprocess).

#!/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
}

Upvotes: 1

Related Questions