# Rescuing a failed %x\[\] call?

**URL:** <https://www.ruby-forum.com/t/rescuing-a-failed-x-call/154793>\
**Category:** Ruby\
**Created:** [December 4, 2008, 12:09am UTC](https://www.ruby-forum.com/t/rescuing-a-failed-x-call/154793 "2008-12-04T00:09:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [December 4, 2008, 12:09am UTC](https://www.ruby-forum.com/t/rescuing-a-failed-x-call/154793/1 "2008-12-04T00:09:08Z")

</div>

I know it should be streightforward, but I’m not finding the docs  
anywhere…

How does one go about rescuing a failed shell command? like in the  
instance it fails, or the command isn’t found?

Thanks,  
Kyle

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [December 4, 2008, 4:34am UTC](https://www.ruby-forum.com/t/rescuing-a-failed-x-call/154793/2 "2008-12-04T04:34:38Z")

</div>

On Wed, Dec 3, 2008 at 6:02 PM, Kyle S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I know it should be streightforward, but I’m not finding the docs anywhere…
> 
> How does one go about rescuing a failed shell command? like in the  
> instance it fails, or the command isn’t found?

According to some experiments in IRB, the failed shell command won’t  
raise an exception; it will leave a non-zero exit code in the $?  
special variable. You’ll need to check the value of $? and take  
action accordingly.

–  
Avdi

Home: [http://avdi.org](http://avdi.org)  
Developer Blog: [Avdi Grimm, Code Cleric](http://avdi.org/devblog/)  
Twitter: [http://twitter.com/avdi](http://twitter.com/avdi)  
Journal: [http://avdi.livejournal.com](http://avdi.livejournal.com)

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [December 4, 2008, 3:05pm UTC](https://www.ruby-forum.com/t/rescuing-a-failed-x-call/154793/3 "2008-12-04T15:05:12Z")

</div>

On Wed, Dec 3, 2008 at 9:28 PM, Avdi G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> According to some experiments in IRB, the failed shell command won’t  
> raise an exception; it will leave a non-zero exit code in the $?  
> special variable. You’ll need to check the value of $? and take  
> action accordingly.
> 
> –  
> Avdi

Advi, the problem is I’ve got a script failing on a “command not  
found”, and not continuing onward. I see no amount of fiddling that  
can help that.

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [December 4, 2008, 4:49am UTC](https://www.ruby-forum.com/t/rescuing-a-failed-x-call/154793/4 "2008-12-04T04:49:16Z")

</div>

From: Kyle S. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# I know it should be streightforward, but I’m not finding the

# docs anywhere…

what is kernel#system?

## botp@botp-desktop:~$ qri kernel#system ------------------------------------------------ Kernel#system system(cmd [, arg, …]) =\> true or false

Executes cmd in a subshell, returning true if the command was  
found and ran successfully, false otherwise. An error status is  
available in $?. The arguments are processed in the same way as  
for Kernel::exec.

```
    system("echo *")
    system("echo", "*")

```

# How does one go about rescuing a failed shell command?

# like in the instance it fails, or the command isn’t found?

basically, you’ll just have to ask it, like

rescue\_command\_here unless system(my\_shell\_command)

lastly, you’ll have to test it (because i just type this answers on my  
inbox w/o testing 🙂

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [December 4, 2008, 3:51pm UTC](https://www.ruby-forum.com/t/rescuing-a-failed-x-call/154793/5 "2008-12-04T15:51:44Z")

</div>

On Thu, Dec 4, 2008 at 8:58 AM, Kyle S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Advi, the problem is I’ve got a script failing on a “command not  
> found”, and not continuing onward. I see no amount of fiddling that  
> can help that.

You may need to use IO.popen() instead, then.

–  
Avdi

Home: [http://avdi.org](http://avdi.org)  
Developer Blog: [Avdi Grimm, Code Cleric](http://avdi.org/devblog/)  
Twitter: [http://twitter.com/avdi](http://twitter.com/avdi)  
Journal: [http://avdi.livejournal.com](http://avdi.livejournal.com)

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [December 4, 2008, 5:52pm UTC](https://www.ruby-forum.com/t/rescuing-a-failed-x-call/154793/6 "2008-12-04T17:52:15Z")

</div>

On Thu, Dec 4, 2008 at 8:58 AM, Kyle S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Advi, the problem is I’ve got a script failing on a “command not  
> found”, and not continuing onward. I see no amount of fiddling that  
> can help that.

Just tried it… it works fine for me, outputs the “command not found”  
message on stderr but keeps right on going… what platform are you  
on?

–  
Avdi

Home: [http://avdi.org](http://avdi.org)  
Developer Blog: [Avdi Grimm, Code Cleric](http://avdi.org/devblog/)  
Twitter: [http://twitter.com/avdi](http://twitter.com/avdi)  
Journal: [http://avdi.livejournal.com](http://avdi.livejournal.com)

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [December 4, 2008, 7:16pm UTC](https://www.ruby-forum.com/t/rescuing-a-failed-x-call/154793/7 "2008-12-04T19:16:17Z")

</div>

On Thu, Dec 4, 2008 at 10:45 AM, Avdi G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Just tried it… it works fine for me, outputs the “command not found”  
> message on stderr but keeps right on going… what platform are you  
> on?
> 
> –  
> Avdi

Linux, CentOS/RedHat. Depending on the box it’ll be anywhere from  
2.1AS all the way through 5.2, so the code is actually run on a rather  
wide variety.

–Kyle

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [December 4, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/rescuing-a-failed-x-call/154793/8 "2008-12-04T15:06:58Z")

</div>

On Wed, Dec 3, 2008 at 9:42 PM, PeÃ±a, Botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Executes cmd in a subshell, returning true if the command was
> 
> basically, you’ll just have to ask it, like
> 
> rescue\_command\_here unless system(my\_shell\_command)
> 
> lastly, you’ll have to test it (because i just type this answers on my inbox w/o testing 🙂

Would work, but I can’t use system because I need to capture and  
process the output of the shell commands.

Thanks though.
