coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem using dd within bash script


From: Bob Proulx
Subject: Re: Problem using dd within bash script
Date: Mon, 28 Apr 2014 12:40:00 -0600
User-agent: Mutt/1.5.23 (2014-03-12)

Sebastian Rückerl wrote:
> Bob Proulx wrote:
> > In your trap handler don't 'exit 1'.  That washes off the correct exit
> > code.  Instead reset your trap handler to the default handler and then
> > send the kill signal back to the current process.  That will cause it
> > to exit with the correct, kill on signal, information.

>          trap '' INT
>          kill -s INT $$

First, I made a mistake and it needs a correction.  I typed that in
when translating to your example but '' ignores the signal.  We don't
want to ignore it.  We want to set it to the default handler.  That
should be - instead.  A big mistake.  It should be:

         trap - INT
         kill -s INT $$

My example that I usually use is:

  unset tmpfile
  cleanup() {
    test -n "$tmpfile" && rm -f "$tmpfile"
  }
  trap "cleanup" EXIT
  # Begin dash specific trap handling.
  trap "cleanup; trap - HUP; kill -HUP $$" HUP
  trap "cleanup; trap - INT; kill -INT $$" INT
  trap "cleanup; trap - QUIT; kill -QUIT $$" QUIT
  trap "cleanup; trap - TERM; kill -TERM $$" TERM
  trap "trap - PIPE; cleanup; kill -PIPE $$" PIPE
  # End dash specific trap handling.
  tmpfile=$(mktemp) || exit 1

> Is this really a big deal as the next process (the one calling this
> script) is the bash / myself starting it on the command line? So is
> this something that is generally important for scripting? I must
> admit, I have never thought about it.

I think it is a big deal.  It is one of slack culture.  It isn't the
correct thing to do.  Yet we have all seen it done many times.  And so
the problem continues and continues.  The only way to stop that
culture is to educate as to the problem so that it becomes the culture
to always handle signals correctly.

Have you ever seen a script that includes a loop and when you
Control-C to interrupt it the script does not stop?  It cycles around
the loop again and does not interrupt.  You end up holding down the
Control-C key to give many key-repeated interrupts.  Eventually one of
them gets through and the script exits.  It just does not behave
correctly.  That is an example incorrect signal handling causing
trouble.  If signals were handled properly then one interrupt would
always be sufficient.

A good article explaining this in detail.

  http://www.cons.org/cracauer/sigint.html

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]