Exiting from sleep within a .bashrc function

This is a discussion on Exiting from sleep within a .bashrc function within the shell forums in Operating Systems category; Within my .bashrc, I have the following function which I would like to exit from sleep, if Control-C is pressed. However, nothing I have tried has worked. Every time it proceeds onto the ls command. I am attempting to trap the signal within a sub-shell by doing this: dosomething() { ( trap 'echo You INTERRUPTED me ;exit' SIGINT ; sleep 5 ) ls } This comes close to working because I can see the message when it runs if I put in another sleep, e.g. dosomething() { ( trap 'echo You INTERRUPTED me ;sleep 2; exit' SIGINT ; sleep 5 ) ls } But it still ignores ...

Go Back   Database Forum > Operating Systems > shell

Database Forums

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-26-2008, 04:02 PM
Default Exiting from sleep within a .bashrc function

Within my .bashrc, I have the following function which I would like to
exit from sleep, if Control-C is pressed.
However, nothing I have tried has worked. Every time it proceeds onto
the "ls" command.

I am attempting to trap the signal within a sub-shell by doing this:

dosomething()
{
( trap 'echo "You INTERRUPTED me";exit' SIGINT ; sleep
5 )
ls
}

This comes close to working because I can see the message when it runs
if I put in another sleep, e.g.

dosomething()
{
( trap 'echo "You INTERRUPTED me";sleep 2; exit' SIGINT ;
sleep 5 )
ls
}

But it still ignores the exit and moves onto the "ls". The question
is why?

I have found a workaround by doing this:

sleep 5 && ls

But I would still love to know why "exit" won't exit in my function.
Thanks in advance.
Reply With Quote
  #2  
Old 08-26-2008, 06:51 PM
Default Re: Exiting from sleep within a .bashrc function

On 2008-08-26, jaredsubman-at-yahoo.com wrote:
> Within my .bashrc, I have the following function which I would like to
> exit from sleep, if Control-C is pressed.
> However, nothing I have tried has worked. Every time it proceeds onto
> the "ls" command.
>
> I am attempting to trap the signal within a sub-shell by doing this:
>
> dosomething()
> {
> ( trap 'echo "You INTERRUPTED me";exit' SIGINT ; sleep
> 5 )
> ls
> }
>
> This comes close to working because I can see the message when it runs
> if I put in another sleep, e.g.
>
> dosomething()
> {
> ( trap 'echo "You INTERRUPTED me";sleep 2; exit' SIGINT ;
> sleep 5 )
> ls
> }
>
> But it still ignores the exit and moves onto the "ls". The question
> is why?
>
> I have found a workaround by doing this:
>
> sleep 5 && ls
>
> But I would still love to know why "exit" won't exit in my function.


It is; it is exiting from the subshell you invoked with ( ... ).

--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Reply With Quote
  #3  
Old 08-27-2008, 09:59 AM
Default Re: Exiting from sleep within a .bashrc function

On Aug 26, 4:51*pm, "Chris F.A. Johnson" wrote:
> On 2008-08-26, jaredsub...@yahoo.com wrote:
> > Within my .bashrc, I have the following function which I would like to
> > exit from sleep, if Control-C is pressed.
> > However, nothing I have tried has worked. *Every time it proceeds onto
> > the "ls" command.

>
> > I am attempting to trap the signal within a sub-shell by doing this:

>
> > dosomething()
> > {
> > * * * *( trap 'echo "You INTERRUPTED me";exit' SIGINT ; sleep
> > 5 )
> > * * * * ls
> > }

>
> > This comes close to working because I can see the message when it runs
> > if I put in another sleep, e.g.

>
> > dosomething()
> > {
> > * * * * ( trap 'echo "You INTERRUPTED me";sleep 2; exit' SIGINT;
> > sleep 5 )
> > * * * * ls
> > }

>
> > But it still ignores the exit and moves onto the "ls". * The question
> > is why?

>
> > I have found a workaround by doing this:

>
> > sleep 5 && ls

>
> > But I would still love to know why "exit" won't exit in my function.

>
> * *It is; it is exiting from the subshell you invoked with ( ... ).
>
> --
> * *Chris F.A. Johnson, author * * *
> * *Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
> * *===== My code in this post, if any, assumes the POSIX locale
> * *===== and is released under the GNU General Public Licence


Yes, but it won't exit the function entirely so that "ls" won't
execute. The question, then is, how do I make that happen?
Reply With Quote
  #4  
Old 08-27-2008, 10:15 AM
Default Re: Exiting from sleep within a .bashrc function

On Wednesday 27 August 2008 14:59, jaredsubman-at-yahoo.com wrote:

>> > I am attempting to trap the signal within a sub-shell by doing this:

>>
>> > dosomething()
>> > {
>> > ( trap 'echo "You INTERRUPTED me";exit' SIGINT ; sleep
>> > 5 )
>> > ls
>> > }


You are putting the trap code in (), so it's executed in a subshell,
and "exit" exits that subshell. But see below.

>> > But I would still love to know why "exit" won't exit in my function.

>>
>> It is; it is exiting from the subshell you invoked with ( ... ).

>
> Yes, but it won't exit the function entirely so that "ls" won't
> execute. The question, then is, how do I make that happen?


From man bash:

"Command substitution, commands grouped with parentheses, and
asynchronous commands are invoked in a subshell environment"

so, it seems that the trap code is executed in a subshell anyway.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
Reply With Quote
Reply


Thread Tools
Display Modes



All times are GMT -4. The time now is 10:20 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Integrated by bbpixel2008 :: jvbPlugin R1013.368.1

Search Engine Friendly URLs by vBSEO 3.1.0
vB Ad Management by =RedTyger=
In an effort to better serve ads to our visitors, cookies are used on Mydatabasesupport.com. For more information, check out our Privacy Policy.