Giving "read" from standard input a timeout.

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 03-02-2005
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  
Old 03-02-2005
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  
Old 03-03-2005
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
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.
# 5  
Old 03-03-2005
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"

# 6  
Old 03-03-2005
rello, what system are you using? If you have the line program, try:
ans=$(line)
rather than
read ans
# 7  
Old 03-03-2005
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.
Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Failure: if grep "$Var" "$line" inside while read line loop Subsonic66 Shell Programming and Scripting 5 10-08-2018 02:08 PM
Using "mailx" command to read "to" and "cc" email addreses from input file asjaiswal UNIX for Dummies Questions & Answers 2 10-11-2014 10:32 PM
HP-UX: Shell Script giving " 0^J30: Syntax error" vai_sh HP-UX 16 03-10-2014 06:28 AM
Bash Script giving "Command Not found" infinitydon Shell Programming and Scripting 6 10-03-2013 01:21 PM
Read from "list1" and list matches in "list2" lewk Shell Programming and Scripting 3 08-13-2012 07:32 AM
Purpose of "read" and "$END$" in ksh ? dbadmin100 Shell Programming and Scripting 10 07-01-2011 03:05 PM
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 09:59 AM
Giving a name to a Terminal in "Xfce" desktop environment hooge789 UNIX for Dummies Questions & Answers 4 12-04-2010 12:02 PM
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
In ksh shell command - Print "-ABC" is giving error sagarjani Shell Programming and Scripting 2 10-08-2008 05:32 PM
How to timeout the "read" command rm-r Shell Programming and Scripting 5 08-15-2008 12:22 PM
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
All times are GMT -4. The time now is 11:49 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.