# How to keep proceeding if one command fails (in Rake)?

**URL:** <https://www.ruby-forum.com/t/how-to-keep-proceeding-if-one-command-fails-in-rake/101206>\
**Category:** Ruby\
**Created:** [August 3, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/how-to-keep-proceeding-if-one-command-fails-in-rake/101206 "2006-08-03T15:28:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![luc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@luc](https://www.ruby-forum.com/u/luc)\
**Post date:** [August 3, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/how-to-keep-proceeding-if-one-command-fails-in-rake/101206/1 "2006-08-03T15:28:06Z")

</div>

Hi All,

Could you tell me how I could do to have rake to run all the tasks no  
matter the result of the previous one(s)?

ex:

file ‘xxx.c’ =\> [‘xxx.h’] do  
if(sh(blablabla))  
puts “do this”  
else  
puts “do this”  
end

end

When for exemple the sh command failed, it quits and does not run the  
rest of the scripts. How can I make this happen?

Thanks,

Luc

---

<div class="post-metadata">

**Author:** ![luc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@luc](https://www.ruby-forum.com/u/luc)\
**Post date:** [August 3, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/how-to-keep-proceeding-if-one-command-fails-in-rake/101206/2 "2006-08-03T16:28:07Z")

</div>

Have you looked into using the begin…rescue…end syntax? It’s  
basically a try/catch (but Ruby doesn’t understand the meaning of  
‘try’… haha).

begin  
sh(blablabla)  
puts “do this”  
rescue  
puts “do this”  
end  
puts “do this”

That is, if sh() raises an exception. Otherwise, you’d have to

raise “Oops” unless sh(blablala)

Give it a try…

M.T.

---

<div class="post-metadata">

**Author:** ![luc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@luc](https://www.ruby-forum.com/u/luc)\
**Post date:** [August 4, 2006, 9:02am UTC](https://www.ruby-forum.com/t/how-to-keep-proceeding-if-one-command-fails-in-rake/101206/3 "2006-08-04T09:02:06Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Matt T. wrote:

> Have you looked into using the begin…rescue…end syntax? It’s  
> basically a try/catch (but Ruby doesn’t understand the meaning of  
> ‘try’… haha).

“Do or do not… there is no try” - Yoda  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFE0vBlmV9O7RYnKMcRAr5qAJ0Suu42JDz2VJdWf3eT8ocbGVNpXQCgmY4D  
RGjAeNvqveC4ABmND+h7yAg=  
=Bc2D  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![luc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@luc](https://www.ruby-forum.com/u/luc)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/how-to-keep-proceeding-if-one-command-fails-in-rake/101206/4 "2006-08-15T14:18:47Z")

</div>

Hi,

At Sun, 13 Aug 2006 12:23:07 +0900,  
Eric A. wrote in [ruby-talk:208107]:

> Unfortunately, we’re talking about system directories  
> I have no control over, that have to be in my  
> search path, so that warning gets to be a bit  
> annoying after a while.

World writable system directories? Hmmm…, amazing.

> I understand why it’s there, but it’s even worse  
> for scripts I distribute: They cause alarm bells  
> go off, right at the time I’m trying to lull  
> people to sleep… (Relax… Sleep… Pay no  
> attention to the ruby under the hood…)

def adventurous!  
verbose, $VERBOSE = $VERBOSE, nil  
yield  
ensure  
$VERBOSE = verbose  
end

adventurous! {system(cmd)}

> The workaround is to code a semi-colon at the  
> end of the command, so:
> 
> ```
> sh "some command;"
> 
> ```
> 
> or  
> value = `some command;`
> 
> But the unfortunate side effect is that a command  
> failure gets swallowed as well, and that keeps  
> Rake from aborting when it rightfully should.

See $!.success?.

---

<div class="post-metadata">

**Author:** ![luc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@luc](https://www.ruby-forum.com/u/luc)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/how-to-keep-proceeding-if-one-command-fails-in-rake/101206/5 "2006-08-15T14:20:27Z")

</div>

Eric A. wrote:

> value = `some command;`
> 
> But the unfortunate side effect is that a command  
> failure gets swallowed as well, and that keeps  
> Rake from aborting when it rightfully should.

It turns out I was (happily) dead wrong about that.  
Either of the following work just fine:

if ($? != 0) then  
…  
exit  
end

-or-

require ‘English’  
if ($CHILD\_STATUS != 0) then  
…  
exit  
endif

---

<div class="post-metadata">

**Author:** ![luc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@luc](https://www.ruby-forum.com/u/luc)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/how-to-keep-proceeding-if-one-command-fails-in-rake/101206/6 "2006-08-15T14:20:31Z")

</div>

There’s one line in there that’s driving me nuts.

removed\_email\_address@domain.invalid wrote:

> def adventurous!  
> verbose, $VERBOSE = $VERBOSE, nil  
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
> yield  
> ensure  
> $VERBOSE = verbose  
> end
> 
> adventurous! {system(cmd)}

What the heck is that line doing??

---

<div class="post-metadata">

**Author:** ![luc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@luc](https://www.ruby-forum.com/u/luc)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/how-to-keep-proceeding-if-one-command-fails-in-rake/101206/7 "2006-08-15T14:18:40Z")

</div>

On a related note…

When you access ENV or execute a subshell, ruby  
gives you a warning if any parent directory in  
any part of your search path is world-writable.

Unfortunately, we’re talking about system directories  
I have no control over, that have to be in my  
search path, so that warning gets to be a bit  
annoying after a while.

I understand why it’s there, but it’s even worse  
for scripts I distribute: They cause alarm bells  
go off, right at the time I’m trying to lull  
people to sleep… (Relax… Sleep… Pay no  
attention to the ruby under the hood…)

The workaround is to code a semi-colon at the  
end of the command, so:

```
sh "some command;"

```

or  
value = `some command;`

But the unfortunate side effect is that a command  
failure gets swallowed as well, and that keeps  
Rake from aborting when it rightfully should.

So:  
I need a way to make the world-writable  
directory warning go away, and at the  
same time make sure that Rake sees shell  
aborts.

Any solutions out there?

---

<div class="post-metadata">

**Author:** ![luc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@luc](https://www.ruby-forum.com/u/luc)\
**Post date:** [August 16, 2006, 12:13am UTC](https://www.ruby-forum.com/t/how-to-keep-proceeding-if-one-command-fails-in-rake/101206/8 "2006-08-16T00:13:00Z")

</div>

Hal F. wrote:

> So he was just saving off $VERBOSE so he  
> could restore it at the end.

Thank you!

I see the parentheses now:

(x, y) = (y, x)

I recall seeing that idiom and highlighting  
it, too. But when I saw it in real life, the  
commas messed me up. I saw it as:

(x), (y = y), (x)

I read your message that way, too, the first  
6 times I looked at it.

It’s interesting how natural language  
processing swaps the precedence of = and ,  
compared to the compiler.

---

<div class="post-metadata">

**Author:** ![luc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@luc](https://www.ruby-forum.com/u/luc)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/how-to-keep-proceeding-if-one-command-fails-in-rake/101206/9 "2006-08-15T14:20:34Z")

</div>

Eric A. wrote:

> > ```
> > yield
> > 
> > ```
> > 
> > ensure  
> > $VERBOSE = verbose  
> > end
> > 
> > adventurous! {system(cmd)}
> 
> What the heck is that line doing??

Haha… it’s a Perl/Ruby idiom…

```
x, y = y, x # swap values of x and y

```

So he was just saving off $VERBOSE so he  
could restore it at the end.

Hal
