The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
check input = "empty" and "numeric" geoffry Shell Programming and Scripting 6 12-13-2007 05:12 AM
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
Which is the most "standard" platform to learn? PointNClickGuy UNIX for Dummies Questions & Answers 5 10-01-2001 12:54 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-02-2005
rello rello is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 4
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.
  #2 (permalink)  
Old 03-02-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
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..
  #3 (permalink)  
Old 03-03-2005
muthukumar muthukumar is offline
Registered User
  
 

Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
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 (permalink)  
Old 07-07-2005
vigsgb vigsgb is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 22
[QUOTE=vgersh99]
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
[QUOTE]

I am on an IBM p570 with AIX 5.3 and when for some reason I can only get a time delay of 20 seconds. If I use WAIT=200 I get twenty seconds as would be expected, however when I use WAIT=300 I get 4 seconds. Can anyone explain this to me?

Thanks
  #5 (permalink)  
Old 07-07-2005
r2007's Avatar
r2007 r2007 is offline
Registered User
  
 

Join Date: Jun 2005
Location: China
Posts: 73
Quote:
I am on an IBM p570 with AIX 5.3 and when for some reason I can only get a time delay of 20 seconds. If I use WAIT=200 I get twenty seconds as would be expected, however when I use WAIT=300 I get 4 seconds. Can anyone explain this to me?
Just guess
WAIT=200 means 200/10=20 seconds
WAIT=300 means (300 mod 256)/10=4.5 secondes
  #6 (permalink)  
Old 07-08-2005
vigsgb vigsgb is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 22
Quote:
Originally Posted by r2007
Just guess
WAIT=200 means 200/10=20 seconds
WAIT=300 means (300 mod 256)/10=4.5 secondes
That seems to be correct as 256 gives a zero wait time. How did you come up with that? or What is the logic behind the calculaton?
  #7 (permalink)  
Old 03-03-2005
rello rello is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 4
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:24 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0