help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: reading from external command


From: Lawrence Velázquez
Subject: Re: reading from external command
Date: Wed, 30 Mar 2022 19:00:00 -0400
User-agent: Cyrus-JMAP/3.5.0-alpha0-4911-g925b585eab-fm-20220323.003-g925b585e

On Wed, Mar 30, 2022, at 6:32 PM, Peng Yu wrote:
> I am not sure how wait for "$!" would work robustly.
>
> In the following example, it can not get the error in the input.
>
> $ cat ./main.sh
> #!/usr/bin/env bash
> # vim: set noexpandtab tabstop=2:
>
> while read -r x; do
>       echo <(exit 42)
> done < <(
>       builtin printf '%s\n' a b c
>       false
> )
> wait "$!"
> echo "$?"
> $ ./main.sh
> /dev/fd/63
> /dev/fd/63
> /dev/fd/63
> 42

Now you've gone and changed the problem by spinning off multiple
asynchronous commands at once.

I don't know how robust this is, but at a minimum you need to access
$! before spawning the asynchronous commands you don't care about:

    % cat main.bash; echo
    printf '%s\n' "$BASH_VERSION"

    {
        pid=$!
        while read -r x; do
            echo <(exit 42)
        done
    } < <(builtin printf '%s\n' a b c; exit 99)

    wait "$pid"
    echo "$?"

    % bash main.bash
    5.1.16(1)-release
    /dev/fd/62
    /dev/fd/62
    /dev/fd/62
    99

-- 
vq



reply via email to

[Prev in Thread] Current Thread [Next in Thread]