Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timeouting the "read" function 1

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Feb 6, 2002
1,851
IL
HI,

I needed to timeout the "read" after 60 seconds,if no response received from the user,and continue the script !
I have used the timer function in background .It works - after 60 seconds the script receives the "TERM" signal.
I trap the "TERM" and issue "break",expecting it to kick me out of the loop that waits for the "read".
However - it does NOT continue,but waits for the user to hit any key on the keyboard to continue.
What is wrong?
====================================
#!/bin/ksh
Background_Timer ()
# sub funciton to run timer in the background
{
sleep 60
#10 minutes timeout
kill $$
}
Background_Timer &
#call sub function in the background
trap “break” TERM
#break the “read” loop if KILL sent by Background_Timer
echo “\nMaintenance will begin shortly, the system will logout from the CDE”
echo “\nType \”A\” to abort or \”C\” to continue : >> \c"
integer DELAY=0

while true ;do
read DECISION
if [[ $DECISION = [Aa] ]] ;then
exit 1
elif [[ $DECISION = [Cc] ]] ;then
break
else
echo “Type \”A\” or \”C\” only ! >> \c”
fi
done

trap - TERM

CONTINUE with the rest of the script ....
"Long live king Moshiach !"
h
 
I have cut'n'pasted your script on to a Solaris 8 box and it seems to work as you expected. What operating system and which version are you using?

I made one small addition, a kill %1 in the continued part of the script to stop the Background_Timer() function from attempting to kill the process anyway. Maybe that's what's happening to your script... it's continuing and being killed anyway? Annihilannic.
 
Could it perhaps be using a 'read' binary instead of a shell builtin, so the signal isn't being trapped?

I don't have access to an AIX system, so I don't think I can be any more help. Good luck! Annihilannic.
 
#!/bin/sh
#my version, works on solaris
STTY=`stty -g`
WAIT=30
printf "enter y/n\b\b\b"
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
-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Very good! The stty man page is very short on detail about the "min" and "time" options... where can I find out more about non canonical input? Annihilannic.
 
try man termio -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
i forgot
c and perl are very very very very better for this
you can LIVE controll every input-char without
waiting for '\n'
i can post a 'c' code
email to priam@freenet.de if interested
:)
-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Hi,

All works,however even though I set the WAIT=600 (expecting the timeout to be 10 minutes),the functions timeouts after 10 seconds.

What is wrong?
===============================
STTY=`stty -g`
WAIT=600
echo “\nMaintenance will begin shortly, the system will logout from the CDE”
echo “\nType \”C\” to cancel , any other key to continue: >> \c"
stty intr '' -icanon min 0 time $WAIT ignbrk -brkint -ixon isig
read ans
stty $STTY
case ${ans:=y} in [cC]*)exit ;; *) ;; esac

Continue with the rest of the script ... "Long live king Moshiach !"
h
 
i tested this on solaris, what os you have?
which stty ?
i will try 600 this evening. -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
The wait time is specified in tenths of a second, so you would need 6000 to wait 10 minutes. However, it seems that the usable limit (on Solaris 8 anyway) is 10 seconds anyway, as you discovered.

Perhaps you will have to go back to your original method and get it working, or use perl or a C programme as jamisar also suggested? Annihilannic.
 
apparently Annihilannic is rigth, 10 seconds and it's returns, also in my 'c' version.... i will investigate it.
i believe levw, you can do this in a very elegant way using tcl and the func 'after'
i will post an example (not actually on my unix box)
:) -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top