Sign in to follow this  

help with bash

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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 0
stty raw
mplayer playfile &
read somechar

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this