The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" iBot UNIX and Linux RSS News 0 01-04-2008 12:00 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-19-2007 10:52 PM
"Trap 3e" OBP error on Ultra 5. akbar SUN Solaris 2 04-17-2007 10:38 PM
"trap" password expiration message MizzGail UNIX for Dummies Questions & Answers 2 03-24-2006 05:50 AM
No utpmx entry: you must exec "login" from lowest level "shell" peterpan UNIX for Dummies Questions & Answers 0 01-18-2006 01:15 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-07-2006
Registered User
 

Join Date: Apr 2005
Posts: 4
Question "trap"ed !

Hi,
I picked up a shell script function to implement a read timeout functionality : Wait for specified number of seconds to read input, else return.

E.g:
read_timeout 5 localvar
This would wait for 5 seconds to get a user input, and will exit if no input is given for 5 seconds. The localvar variable would become null in the timed out case.

The code goes as :

read_timeout() {
trap : USR1
trap 'kill "$pid" 2> /dev/null' EXIT
(sleep "$1" && kill -USR1 "$$") & pid=$!
read "$2"
ret=$?
kill "$pid" 2> /dev/null
trap - EXIT
return "$ret"
}

Inside this function, I want to set the input variable to a default value if the time out happens.

E.g. :
With the statement
read_timeout 5 localvar

localvar should become equal to "nothing" if I do not enter any value for 5 seconds. This is not happening if a set localvar = nothing before invoking the read_timeout function

Is it possible with this piece of code?


Thanks,
Puneet Arora
Reply With Quote
Forum Sponsor
  #2  
Old 07-07-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
bash has a read -t seconds builtin. It does exacatly what you want.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0