Giving "read" from standard input a timeout. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Giving "read" from standard input a timeout.

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-02-2005
rello rello is offline
Registered User
 
Join Date: Mar 2005
Last Activity: 4 March 2005, 11:09 AM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Giving "read" from standard input a timeout.

I want to prompt a user for input but I want it to timeout after a specified time if no response is given. I tried the sleep command but this does not work. I am using ksh.
Thanks.
Sponsored Links
    #2  
Old 03-02-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 22 November 2014, 3:53 PM EST
Location: Foxborough, MA
Posts: 7,668
Thanks: 153
Thanked 586 Times in 551 Posts
Quote:
Originally Posted by rello
I want to prompt a user for input but I want it to timeout after a specified time if no response is given. I tried the sleep command but this does not work. I am using ksh.
Thanks.

Code:
#!/bin/sh
#
STTY=`stty -g`
WAIT=60
printf 'enter [y/n] '
stty intr '' -icanon min 0 time $WAIT ignbrk -brkint -ixon isig
read ans
stty $STTY
case ${ans:=y} in [yY]*) ;; *) exit ;; esac
echo do the rest
exit

or read timeout

Last edited by vgersh99; 03-02-2005 at 04:49 PM..
Sponsored Links
    #3  
Old 03-03-2005
muthukumar muthukumar is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 8 November 2005, 6:07 AM EST
Location: Coimbatore, Tamilnadu, India
Posts: 119
Thanks: 0
Thanked 1 Time in 1 Post
You can use read timeout functionality to do this. We can make your requirement as,


Code:
#!/bin/sh
# input timeout

echo "Enter your input"
read -t 5 input
if [[ $? -ne 0 ]]
then
  echo "User did not enter any input"
  exit 1
else
  echo "User did input as $input"
fi

exit 0
# END #

Try this in bash.

HTH.
    #4  
Old 03-03-2005
rello rello is offline
Registered User
 
Join Date: Mar 2005
Last Activity: 4 March 2005, 11:09 AM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you both for your help. I am still having trouble since neither works with ksh.
I have tried modifying both and still am not having much luck.
Sponsored Links
    #5  
Old 03-03-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 22 November 2014, 3:53 PM EST
Location: Foxborough, MA
Posts: 7,668
Thanks: 153
Thanked 586 Times in 551 Posts
Quote:
Originally Posted by rello
Thank you both for your help. I am still having trouble since neither works with ksh.
I have tried modifying both and still am not having much luck.
strange - works just fine for me under ksh on Solaris.
have you looked at the link I've posted?

here's another way - quoted from the posted link:

Code:
#!/bin/ksh

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"
}
read_timeout 5 var
printf 'Got: "%s" as $var\n' "$var"

Sponsored Links
    #6  
Old 03-03-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 20 November 2014, 5:33 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 438 Times in 261 Posts
rello, what system are you using? If you have the line program, try:
ans=$(line)
rather than
read ans
Sponsored Links
    #7  
Old 03-03-2005
rello rello is offline
Registered User
 
Join Date: Mar 2005
Last Activity: 4 March 2005, 11:09 AM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
I am running Solaris 9. All the programs are in ksh not /bin/sh.
I am also not trying to kill the process.
I have menu and one othe optoins calls a script that loops and pauses 5 seconds before repeating.
If we enter Ctrl d or break out we go back to the prompt and not the original menu.
I want to put a prompt at the end of the loop where it pauses (sleeps) for 5 seconds and prompt the user to quit (evaluate and quit) getting me back to the original menu.
I am not sure how ans=$(line) would work.
Thanks.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Manipulating input into the shell "read" command kingpin2502 Shell Programming and Scripting 2 09-15-2009 05:09 AM
read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell wiseguy Shell Programming and Scripting 9 01-28-2009 01:20 PM
How to timeout the "read" command rm-r Shell Programming and Scripting 5 08-15-2008 12:22 PM
Breaking input with "read" command vino Shell Programming and Scripting 2 08-04-2005 01:10 PM
how to request a "read" or "delivered" receipt for mails plelie2 Shell Programming and Scripting 1 08-06-2002 04:26 PM



All times are GMT -4. The time now is 08:00 AM.