PDA

View Full Version : [SOLVED] bash for key or timeout



cazz
January 10th, 2018, 06:04 PM
If it possible in a bash script make a if with timeout so if I don't push a button on the keyboard and the 5 sec have gone it going to do run a command, and if I do press a key it does not run the command.

TheFu
January 10th, 2018, 08:42 PM
I think 'read' has a timeout option in bash. It is a built-in. It is NOT POSIX.

cazz
January 10th, 2018, 08:47 PM
hmm thanks :)

sudodus
January 10th, 2018, 08:57 PM
help read|less

tells us to use read with the option -t


-t timeout time out and return failure if a complete line of input is
not read within TIMEOUT seconds. The value of the TMOUT
variable is the default timeout. TIMEOUT may be a
fractional number. If TIMEOUT is 0, read returns immediately,
without trying to read any data, returning success only if
input is available on the specified file descriptor. The
exit status is greater than 128 if the timeout is exceeded

cazz
January 10th, 2018, 09:19 PM
mm yes I did find that and now I know what to search for then it easy to find the information I need.

Thanks alot :)

TheFu
January 11th, 2018, 12:01 AM
BTW, I didn't know about the 'help' command until sudodus wrote it above. ;) there is always something new to learn.

The trick for finding help is to know if the command is 'built-in' or external. External commands should have manpages or info-pages. man / info. info documented commands normally have a manpage stub, which points to the info-page. Built-in commands, like 'read', are part of the bash help, so only those built-in commands are covered.

Other built-in examples are 'for' and 'while', which are good for looping in bash scripts.
Other shells might not have a help command.