bug-bash
[Top][All Lists]
Advanced

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

Re: Weird -s test behavior


From: Grisha Levit
Subject: Re: Weird -s test behavior
Date: Fri, 23 May 2025 17:27:52 -0400

On Fri, May 23, 2025, 17:17 Stephen Leaf <smileaf@gmail.com> wrote:

>
> #!/bin/bash
> TMPFILE=$(mktemp /tmp/tmp.XXXX)
> for a in this is a test; do
>         if [ $a == 'a' ]; then
>                 >&2 echo b
>         fi
> done 2> >(echo "test!"> "$TMPFILE")
>

The process substitution is asynchronous, so the write may not complete by
the time the next command executes.

Try adding "wait $!" here to wait for it to complete.

>
> #touch "$TMPFILE"
> if [ -s "$TMPFILE" ]; then
>         echo temp file is not empty
> else
>         echo temp file is empty
> fi
> echo "Contents of $TMPFILE"
> cat $TMPFILE
> rm $TMPFILE
>
> I get the output of:
> temp file is empty
> Contents of /tmp/tmp.LGvR
> test!
>
> if I touch or cat the TMPFILE before the test, it behaves as expected.
>


reply via email to

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