help with bash

This topic is 4182 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

hi, i am making a bash script in linux and i want to know is there a way if i can tell if a key has been hit. i dont want the terminal waiting for a keypress. i would do this in c++, but i dont think kbhit() works in linux because g++ gives the error: 'kbhit' was not declared in this scope. any help would be good. thanks in advanced.

Share on other sites
Quote:
 Original post by 31337noobhi, i am making a bash script in linux and i want to know is there a way if i can tell if a key has been hit. i dont want the terminal waiting for a keypress.i would do this in c++, but i dont think kbhit() works in linux because g++ gives the error: 'kbhit' was not declared in this scope.any help would be good.thanks in advanced.

this is probably better discussed in the "everything unix" forums, however what's the context for this? why does it have to be bash?
given the relatively sparse description that you've provided so far, bash isn't really suited for this sort of thing, nonetheless bash itself is based on the GNU readline library, so you could use it from bash, too.
But we really need to know more about what exactly you are trying to do and WHY.

Share on other sites
well, i am making an alarm clock.

crontab takes care of the time.

when it is time, it runs a bashscript

and that bashscript

opens konsole

and runs mplayer with a song

it will repeat untill a key has been pressed

and i just want to know how to get that key press without the konsole waiting for a keypress.

Share on other sites
Quote:
 Original post by 31337noobwell, i am making an alarm clock.crontab takes care of the time.when it is time, it runs a bashscriptand that bashscript opens konsole and runs mplayer with a song

Why not just have your cron job run mplayer directly, and have the user close the mplayer window instead?

As for your original question, the answer is that you'll have to put the controlling tty in raw mode and do a read on file descriprot zero. You'll want to have an exit function hooked in to your script to reset the tty mode.

Something like this (untested)
cleanup(){  stty -raw}trap cleanup 0stty rawmplayer playfile &read somechar

• 9
• 48
• 11
• 17
• 11