Re: [ILUG] bash bug?

From: Brian Foster (blf at domain utvinternet.ie)
Date: Fri 14 Jun 2002 - 15:44:32 IST


  | Date: Fri, 14 Jun 2002 12:12:14 +0100
  | From: Stephen Shirley <diamond at domain skynet.ie>
  |
  | [ ... The bash script is ] supposed to allow ctrl-c to be pressed during
  | the sleep_60 function, which should just return, and all it well.
  | However, it only works the first time. The next time sleep_60 is called
  | [ the trap does not appear to be taken ... ].
  |
  | So, can anyone see any problems [ ... ] or is it actually a bash bug (or
  | even something else)?

 interesting. at first glance, and sans googling, it does
 sort of look like a bash(1) problem ....

 fix (work-around) is trivial (tested with bash 2.05.0(1)
 on SuSE 7.3; but may _not_ work other Bourne-ish shells,
 due to various syntax issues such as the `local' builtin
 and `return -1'):

=====(cut here and below)=====(working variant of original)=====(bash script)=====
#!/bin/bash

function sleep_60 {
    local i=60
    trap 'i=-1' INT
    echo -n "Died ... restarting in "
    while [ $i -gt 0 ]; do
        echo -n "$i"
        i=$((i-1))
        sleep 1
        echo -n ' '
    done
    echo NOW
        # unlike ksh(1), bash(1) traps are global, so be sure to clean up in
        # _all_ cases (i.e., whether or not the trap is taken) --- otherwise
        # there will be a lurking reassignment of `i' on any subsequent INT
        # (almost certainly not what is wanted!) ...
        #
        # N.b. following resets the INT handler to the original value when
        # the script started, which is not always what's wanted ;-(
        #
    trap - INT
    return $i
}

while true; do
    #... do stuff
    sleep_60
done
=====(cut here and above)=====(working variant of original)=====(bash script)=====

 the comment within the above work-around also points out
 a problem in the original script --- it left the `return'
 handler active for any SIGINT (e.g., ctrl-C) subsequent to
 the first invocation of sleep_60, which is very unlikely
 to be desired!

cheers,
        -blf-

--
 Innovative, very experienced, Unix and      | Brian Foster    Dublin, Ireland
 Chorus (embedded RTOS) kernel internals     | e-mail: blf at domain utvinternet.ie
 expert looking for a new position ...       | mobile: (+353 or 0)86 854 9268
  For a résumé, contact me, or see my website  http://www.blf.utvinternet.ie
    Stop E$$o (ExxonMobile):  «Whatever you do, don't buy Esso --- they
     don't give a damn about global warming.»    http://www.stopesso.com
     Supported by Greenpeace, Friends of the Earth, and numerous others...


This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:17:16 GMT