#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    UK
    Posts
    18
    Rep Power
    0

    Trappable Signals


    Hi All,

    I'm looking for a way to have a script prompt the user for some input but only wait a certain amount of time (20 secs) before assuming a default response.

    The approach I'm trying is to start a background timer process that sleeps for 20 seconds then sends a signal to the parent. A trap in the parent should pick up the signal and allow me to bypass the read that's requesting input. The problem I'm having with this is that the signal my background timer is sending is always ignored until the read command has terminated. Obviously this isn't what I want.

    So, does anyone know of:
    1. A signal (other than -9) that will actually interrupt a "read", or
    2. A better way of doing what I want?

    Many thanks

    Steve
    (I'm on AIX 5.1 using ksh if that helps)
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,782
    Rep Power
    4301
    Try using eread from here.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    UK
    Posts
    18
    Rep Power
    0
    Thanks for that.

    eread seems to be using basically the same technique as me but instead of the ALRM, USR1 or USR2 signals that I've been using it's using INT. Simple really. I would have got there eventually.

    If anyone is interested this is what I've ended up with:

    Code:
    # Trap INT signals but do nothing with them
    trap 'true' INT
    
    # Display some stuff to the user
    ...
    
    # Start a background timer that sends an INT after 20 seconds
    sleep 20 && kill -s INT $$ > /dev/null 2>&1 &
    
    # Now get user feedback
    read response?"Response? : "
    
    # Kill the timer
    kill $! 2> /dev/null
    
    # Act on the response
    ...
    This code will wait 20 seconds for a user response. If non is received then $response will be null and you can imply whatever you want from that.

    Steve
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Prague, Czech Rep.
    Posts
    117
    Rep Power
    15
    I propose another solution which makes use of the noncanonical mode. I have tested it on AIX 5 2.


    :
    echo Press any key within 20 sec. to start input
    stty -echo -icanon min 0 time 200 # 200 tenths of second
    ANS=$(dd count=1 bs=100 2>/dev/null)
    stty echo icanon
    if [ -z "$ANS" ] ; then
    echo no response, default values used
    else
    echo input required values
    fi


    Regards
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Location
    UK
    Posts
    18
    Rep Power
    0
    Very handy. Many thanks.

IMN logo majestic logo threadwatch logo seochat tools logo