LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-23-2009, 02:55 PM   #1
steven.c.banks
Member
 
Registered: Dec 2007
Location: Virginia
Distribution: RHEL
Posts: 44
Blog Entries: 1

Rep: Reputation: 15
setting a timeout in an interactive bash shell script


I have a bash shell script that interacts with users, asking questions using echo and getting their replies using read. I need a way to exit the script if the user has not replied in some fixed period, e.g. five minutes.
If possible, I would like this to be global - i.e. set the timeout value once at the beginning of the script, instead of with each echo that asks for a response.
Any ideas? Thanks in advance...
 
Old 01-23-2009, 03:17 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
The easy way is to use the -t option of read to specify a timeout in seconds for each input statement. The more complex way is one suggested by the Advanced Bash Scripting Guide, which defines a functions that can be used further through the script and its childs:
Code:
timedout_read() {
  timeout=$1
  varname=$2
  old_tty_settings=`stty -g`
  stty -icanon min 0 time ${timeout}0
  eval read $varname      # or just  read $varname
  stty "$old_tty_settings"
}
but as you can see its usage is quite the same as read -t. This function shows the correct global settings, anyway. Reference: http://www.tldp.org/LDP/abs/html/int...s.html#TIMEOUT
 
Old 01-23-2009, 04:07 PM   #3
steven.c.banks
Member
 
Registered: Dec 2007
Location: Virginia
Distribution: RHEL
Posts: 44
Blog Entries: 1

Original Poster
Rep: Reputation: 15
Thanks!! The read -t works perfectly. After the timeout, I can check the value of the read variable, and exit the script if it is null.
 
  


Reply

Tags
timeout


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting Up an Interactive Section within a Shell Script kaplan71 Linux - Software 2 09-02-2008 12:29 PM
BASH - How to open an interactive script from a non interactive script..... OldGaf Programming 4 06-29-2008 04:34 PM
shell script in non interactive way googlix Linux - General 6 11-13-2007 09:18 AM
script connecting to erlang shell works only in interactive mode ineya Linux - Server 1 10-25-2007 07:05 AM
shell script read non-interactive comtmr Linux - General 6 11-01-2006 06:54 AM


All times are GMT -5. The time now is 03:32 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration