bug-bash
[Top][All Lists]
Advanced

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

Re: Why does ctrl-c behave differently for builtin sleep in a subshell?


From: Peng Yu
Subject: Re: Why does ctrl-c behave differently for builtin sleep in a subshell?
Date: Tue, 25 Dec 2018 10:28:22 -0600

If I add a command after the builtin sleep, then the EXIT trap will be
triggered upon typing ctrl-C.

If  the last command is removed, then the EXIT trap will not be
triggered upon typing ctrl-C.

Is this a bug in the built-in sleep? I am trying to inspect the C code
of sleep builtin, but it is very simple. I am not sure where the
problem comes from.

Could anybody take a look? Thanks.

$ cat main_INT.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

source ../../../trapexit.sh
(
    trapexit -q <<-'EOF'
    echo Hello World!
    EOF
    source enable.sh
    enable sleep
    sleep 10
    echo "$?"
)
$   ./main_INT.sh
^CHello World!

$ cat main_INT.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

source ../../../trapexit.sh
(
    trapexit -q <<-'EOF'
    echo Hello World!
    EOF
    source enable.sh
    enable sleep
    sleep 10
    #echo "$?"
)
$ ./main_INT.sh
^C
$ source enable.sh
$ enable sleep
$ help sleep
sleep: sleep seconds[.fraction]
    Suspend execution for specified period.
    sleep suspends execution for a minimum of SECONDS[.FRACTION] seconds.

On Mon, Dec 17, 2018 at 1:57 PM Peng Yu <pengyu.ut@gmail.com> wrote:
>
> Note that when SIGINT is set, whether an external or built-in sleep is
> used will not result in any difference.
>
> If I use some other method (say a for loop) instead of built-in sleep
> to introduce a delay, the EXIT can be reached upon ctrl-c. So this
> sounds like that the builtin sleep somehow causes the problem. Is it
> the case?
>
> $ cat main_builtin_INT.sh
> #!/usr/bin/env bash
> trap 'echo at EXIT' EXIT
> trap 'echo at SIGINT' SIGINT
> enable -f ~/Downloads/bash-4.4/examples/loadables/sleep sleep
> sleep 10
> $ cat  \(/main_builtin_INT.sh
> #!/usr/bin/env bash
> (
>     trap 'echo at EXIT' EXIT
>     enable -f ~/Downloads/bash-4.4/examples/loadables/sleep sleep
>     sleep 10
> )
> $ ./main_builtin_INT.sh
> ^Cat SIGINT
> at EXIT
> $ '('/main_builtin_INT.sh
> ^Cat SIGINT
> at EXIT
>
> $ cat ./main_builtin_for.sh
> #!/usr/bin/env bash
> (
>     trap 'echo at EXIT' EXIT
>     for ((i=0;i<100000;++i)); do
>         :
>     done
> )
> $  ./main_builtin_for.sh
> ^Cat EXIT
>
> On Mon, Dec 17, 2018 at 1:41 PM Peng Yu <pengyu.ut@gmail.com> wrote:
> >
> > Hi,
> >
> > See the follow code, '(/main_builtin.sh' behaves differently from the
> > other three scripts upon receiving ctrl-c. This is counter-intuitive.
> > I'd expect whether a built-in command called the result should be the
> > same.
> >
> > How to understand why there is such a difference? Is there an easy
> > workaround to make the result the same as the other three?
> >
> > $   ./main.sh
> > ^Cat EXIT
> >
> > $   ./main_builtin.sh
> > ^Cat EXIT
> >
> > $ \(/main.sh
> > ^Cat EXIT
> >
> > $ \(/main_builtin.sh
> > ^C
> >
> > $ cat main.sh
> > #!/usr/bin/env bash
> > trap 'echo at EXIT' EXIT
> > sleep 10
> >
> > $ cat main_builtin.sh
> > #!/usr/bin/env bash
> > trap 'echo at EXIT' EXIT
> > enable -f ~/Downloads/bash-4.4/examples/loadables/sleep sleep
> > sleep 10
> >
> > $ cat '(/main.sh'
> > #!/usr/bin/env bash
> > (
> >     trap 'echo at EXIT' EXIT
> >     sleep 10
> > )
> >
> > $ cat '(/main_builtin.sh'
> > #!/usr/bin/env bash
> > (
> >     trap 'echo at EXIT' EXIT
> >     enable -f ~/Downloads/bash-4.4/examples/loadables/sleep sleep
> >     sleep 10
> > )
> >
> > --
> > Regards,
> > Peng
>
>
>
> --
> Regards,
> Peng



-- 
Regards,
Peng



reply via email to

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