Simulate wait's command -n flag in zsh
Asked Answered
J

1

6

I am creating two shell jobs as follows

sleep 5 &
completion_pid=$!

sleep 40 && exit 1 &
failure_pid=$! 

In bash I am able to get the exit code of the first job to finish by using the -n flag of wait's command

# capture exit code of the first subprocess to exit
wait -n $completion_pid $failure_pid

It seems however that this flag is not available in my MacOS Big Sur's version of wait (probably cause I am using zsh - ? )

▶ wait -n
wait: job not found: -n

Are there any alternative tools to do this that are also available on MacOS?

What perhaps is weird is that I am getting the same error when invoking a script containing wait -n as bash myscript.sh...

Jessee answered 6/9, 2021 at 23:14 Comment(2)
wait -n was a relatively new addition to bash (introduced in 4.3). Are you using an older version of bash? (And regarding your original question, I'm not aware of any simple way to simulate wait -n in zsh.)Agranulocytosis
thanks, you were right about the bash version; upgrading it allows me to at leas run my script with bash interpreterJessee
A
-1

Since you are waiting by specifying PIDs, you can simply do a

 wait $completion_pid $failure_pid
Adamec answered 8/9, 2021 at 8:47 Comment(2)
This will wait until even the last job of the two has exited; I think -n will wait until the first has exitedJessee
Indeed, you are right. I didn't recognize this from my first reading of the bash man page.....Adamec

© 2022 - 2024 — McMap. All rights reserved.