Forum: Ruby rescuing a failed %x[] call?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kyle S. (Guest)
on 2008-12-04 01:09
(Received via mailing list)
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
Avdi G. (Guest)
on 2008-12-04 05:34
(Received via mailing list)
On Wed, Dec 3, 2008 at 6:02 PM, Kyle S. <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
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com
Peña, Botp (Guest)
on 2008-12-04 05:49
(Received via mailing list)
From: Kyle S. [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 :)
Kyle S. (Guest)
on 2008-12-04 16:05
(Received via mailing list)
On Wed, Dec 3, 2008 at 9:28 PM, Avdi G. <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.
Kyle S. (Guest)
on 2008-12-04 16:06
(Received via mailing list)
On Wed, Dec 3, 2008 at 9:42 PM, Peña, Botp 
<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.
Avdi G. (Guest)
on 2008-12-04 16:51
(Received via mailing list)
On Thu, Dec 4, 2008 at 8:58 AM, Kyle S. <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
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com
Avdi G. (Guest)
on 2008-12-04 18:52
(Received via mailing list)
On Thu, Dec 4, 2008 at 8:58 AM, Kyle S. <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
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com
Kyle S. (Guest)
on 2008-12-04 20:16
(Received via mailing list)
On Thu, Dec 4, 2008 at 10:45 AM, Avdi G. <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
This topic is locked and can not be replied to.