Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Using coproc with read in non-interactive shells for sleep alternative #135

Open
andryyy opened this issue Jan 31, 2023 · 0 comments
Open

Comments

@andryyy
Copy link

andryyy commented Jan 31, 2023

Hi,

First of all: THANK YOU! So glad I found this.

I noticed I run in a lot of defunct procs when using the sleep alternative in your docs, I don't know why yet, though.

As an alternative I found it useful to use coproc to give the sub process a stdin:

delay() {
  declare -i i=$1
  coproc d {
    read -n1 -s -t$i
  }
  wait $!
}

This can be modified to be used in a creative way!

In this example I spawn a 30s delay and, while it is already counting down, do some other things in the meantime that are not related to the delay.
When I'm done doing things, I will wait for the remaining delay time:

delay() {
  declare -i i=$1
  coproc d {
    read -n1 -s -t$i
  }
}

delay 30
while [ -v d ]; do
  echo "Doing things"
  wait
done

You'd also be able to stop the delay prematurely by sending input to the pipe: echo > /dev/fd/${d[1]}

André

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant