Timeline for Get exit status of process that's piped to another
Current License: CC BY-SA 3.0
9 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Sep 25, 2024 at 21:38 | comment | added | jlliagre |
@mitchellJ Correct, and mktemp isn't POSIX either. Let's say creating a unique temporary file is left as an exercise for the reader. A POSIX compliant shell that does just that would not be that hard to implement. Note also that it should use $TMPDIR if defined, instead of the hardcoded /tmp.
|
|
| Sep 25, 2024 at 18:55 | comment | added | mitchellJ | The second edit is no longer POSIX as $RANDOM is not POSIX. | |
| Mar 2, 2016 at 7:12 | history | edited | jlliagre | CC BY-SA 3.0 |
added 153 characters in body
|
| Aug 29, 2012 at 22:25 | comment | added | dubiousjim |
You can save a few subshells: (s=/tmp/.$$_$RANDOM;{foo;echo $?>$s;}|bar; exit $(cat $s;rm $s)). @Johan: I agree it's easier with Bash but in some contexts, knowing how to avoid Bash is worth it.
|
|
| Jun 10, 2011 at 16:11 | history | edited | jlliagre | CC BY-SA 3.0 |
added 144 characters in body
|
| Jun 9, 2011 at 6:36 | comment | added | Johan | +1 Well the ${PIPESTATUS[0]} is easier but the basic idea here do work if one know about the problems that Gilles mentions. | |
| Jun 8, 2011 at 23:00 | comment | added | Gilles 'SO- stop being evil' | This doesn't work for several reasons. 1. The temporary file may be read before it's written. 2. Creating a temporary file in a shared directory with a predictable name is insecure (trivial DoS, symlink race). 3. If the same script uses this trick several times, it'll always use the same file name. To solve 1, read the file after the pipeline has completed. To solve 2 and 3, use a temporary file with a randomly-generated name or in a private directory. | |
| Jun 8, 2011 at 20:45 | history | edited | jlliagre | CC BY-SA 3.0 |
added 186 characters in body
|
| Jun 2, 2011 at 23:17 | history | answered | jlliagre | CC BY-SA 3.0 |