| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
|
| 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. |
|
#2
|
| 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 |
|
#3
|
| On Aug 26, 4:51*pm, "Chris F.A. Johnson" > 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? |
|
#4
|
| 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. |
![]() |
| Thread Tools | |
| Display Modes | |