LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Getting asynchronous user input in BASH (https://www.linuxquestions.org/questions/linux-software-2/getting-asynchronous-user-input-in-bash-640919/)

jakeo25 05-08-2008 06:11 PM

Getting asynchronous user input in BASH
 
Hello

I have a BASH script running and would like to stop, without using Ctrl-C (this shuts down the entire script, which is not desired). So I need a clean exit from a running function. The bash built-in 'read' only accepts user input when prompted, and has a minimum of 1 second wait. It is not as 'clean' as I'd like ...(just hold down a key until the function execution catches up to the read ).

So here is what I would like: if the user presses a key at ANY time while a particular function is running, this key stroke would be caught and 'remembered'. The function would periodically check a variable or external event inside this loop, then do a clean exit from the function.

Any key should work, but a specific escape sequence like Ctrl-<something> would be fine.

Thanks in advance.

sed s|jake|Superjake|g

jailbait 05-08-2008 06:22 PM

Depending on how the logic of your program handles the asynchronous input you might be able to do what you want with the trap command.

http://linux.die.net/man/1/trap

Another approach would be to make the asynchronous read a separate thread which signals the main thread when input has been received.

---------------------
Steve Stites


All times are GMT -5. The time now is 01:49 AM.