使用%x捕获命令行错误 [英] Catching command-line errors using %x

查看:152
本文介绍了使用%x捕获命令行错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当您想在命令行上执行某些操作时,您可以使用以下语法:

 %x运行)

但是,我想捕获错误或至少获取响应,正确。我尝试设置:

  result =%x(命令运行)
pre>

并使用try-catch

  begin 
%x(命令运行)
rescue
无效
end

无效。

解决方案

因此,这不能直接回答你的问题t捕获命令的输出)。但是,您可以只检查退出代码( $?),而不是尝试 begin / rescue ):

 %x(命令运行)
除非$? == 0
ack!error occurred
end

编辑:刚刚记住这个新项目。我认为它完全符合您的需要:



https:/ /github.com/envato/safe_shell


Whenever you want to execute something on the command line, you can use the following syntax:

%x(command to run)

However, I want to catch an error or at least get the response so I can parse it correctly. I tried setting:

result = %x(command to run)

and using a try-catch

begin
  %x(command to run)
rescue
  "didn't work"
end

to no avail. How can I capture the results instead of having them printed out?

解决方案

So this doesn't directly answer your question (won't capture the command's output). But instead of trying begin/rescue, you can just check the exit code ($?) of the command:

%x(command to run)
unless $? == 0
   "ack! error occurred"
end

Edit: Just remembered this new project. I think it does exactly what you want:

https://github.com/envato/safe_shell

这篇关于使用%x捕获命令行错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆