Rescuing a failed %x[] call?


#1

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


#2

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


#3

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.


#4

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 :slight_smile:


#5

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


#6

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


#7

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


#8

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 :slight_smile:

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

Thanks though.