help-bash
[Top][All Lists]
Advanced

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

Re: reading from external command


From: Alex fxmbsw7 Ratchev
Subject: Re: reading from external command
Date: Thu, 31 Mar 2022 01:04:02 +0200

bash peng

28250
/dev/fd/63
/dev/fd/63
41
44
44

#!/bin/bash

printf $$\\n

 pid() {
x=( /proc/self/task/* )
printf %s\\n ${x##*/}
 }

>p
 while read -r x ; do
printf %s\\n <( pid >>p ; exit 44 )
 done < <( pid >>p ; printf shit\\nbla\\n ; exit 41 )

 while read -r p ; do
wait $p
echo $?
 done <p


On Thu, Mar 31, 2022 at 1:00 AM Lawrence Velázquez <vq@larryv.me> wrote:
>
> 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]