help with bash

Started by
2 comments, last by Bregma 17 years, 7 months ago
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.
Advertisement
Quote:Original post by 31337noob
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.


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.
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.

Quote:Original post by 31337noob
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


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

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement