|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
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
exitor read timeout Last edited by vgersh99; 03-02-2005 at 03:49 PM.. |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
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
|
||||
|
||||
|
rello, what system are you using? If you have the line program, try:
ans=$(line) rather than read ans |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 04:09 AM |
| read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell | wiseguy | Shell Programming and Scripting | 9 | 01-28-2009 12:20 PM |
| How to timeout the "read" command | rm-r | Shell Programming and Scripting | 5 | 08-15-2008 11:22 AM |
| Breaking input with "read" command | vino | Shell Programming and Scripting | 2 | 08-04-2005 12:10 PM |
| how to request a "read" or "delivered" receipt for mails | plelie2 | Shell Programming and Scripting | 1 | 08-06-2002 03:26 PM |
|
|