简体   繁体   English

验证终端命令的最佳方法是什么在Rails中成功运行?

[英]What is the best way to validate a terminal command has run successfully in Rails?

I'm writing a quick Rails app and was wondering how I can validate the success an exec'd command. 我正在写一个快速的Rails应用程序,并想知道如何验证exec'd命令的成功。 The two commands I'm running are and SVN update, and a cp from one directory to another. 我正在运行的两个命令是SVN更新,以及从一个目录到另一个目录的cp。

If you use the Kernel.system() method it will return a boolean indicating the success of the command. 如果使用Kernel.system()方法,它将返回一个指示命令成功的布尔值。

result = system("cp -r dir1 dir2")
if(result)
#do the next thing
else
# handle the error

There is a good comparison of different ruby system commands here . 有不同Ruby系统的比较好的命令在这里

How are you executing the external commands? 你是如何执行外部命令的? The Ruby system() function returns true or false depending on whether the command was successful. Ruby system()函数返回truefalse具体取决于命令是否成功。 Additionally, $? 另外, $? contains an error status. 包含错误状态。

  1. Just to be pedantic, you can't validate an exec 'd command because exec replaces the current program with the exec 'd command, so the command would never return to Ruby for validation. 只要是迂腐的,你无法验证的exec “因为d命令exec替换为当前程序exec ” d命令,因此命令将不会返回到Ruby进行验证。
  2. For the cp, at least, you would probably be better of using the FileUtils module (part of the Ruby Standard Library), rather than dropping to the shell. 对于cp,至少,您可能更好地使用FileUtils模块(Ruby标准库的一部分),而不是放到shell。
  3. As noted above, the $? 如上所述, $? predefined variable will give you the return code of the last command to be executed by system() or the backtick operator. 预定义变量将为您提供system()或反引号运算符要执行的最后一个命令的返回码。

For SVN update, check the version number before and after the update. 对于SVN更新,请检查更新前后的版本号。

svn_start_version = IO.popen("svn info").readlines[4]
`svn update`
svn_end_version = IO.popen("svn info").readlines[4]
if svn_end_version > svn_start_version
  "success"
end

For the cp, you could do a filesize check on the original file being equal to the copied file. 对于cp,您可以对原始文件执行文件大小检查,使其等于复制的文件。

source_file_size = IO.popen("du file1").readlines
`cp file1 file2`
dest_file_size = IO.popen("du file2").readlines
if dest_file_size == source_file_size
  "success"
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Rails中验证关联的最佳方法是什么? - What is the best way to validate association in Rails? 在Rails 5中运行后台进程的最佳方法是什么? - What is the best way to run a background process in Rails 5? 在 Rails 中验证多封电子邮件和处理错误的最佳方法是什么? - What's the best way to validate multiple emails and handle errors in Rails? 在Rails中建模“ has_two”关系的最佳方法是什么 - What is the best way to model a `has_two` relationship in Rails Rails:建模此has_and_belongs_to_many关联的最佳方法是什么 - Rails: what is the best way to model this has_and_belongs_to_many association 在与Rails应用程序相同的域上运行Wordpress的最佳方法是什么? - What's the best way to run Wordpress on the same domain as a Rails application? 使用Ruby On Rails在Heroku上运行长任务的最佳方法是什么? - What is the best way to run a long task on Heroku with Ruby On Rails? 在Rails中运行PHP的最佳方法是什么? - What's the best way to run PHP within Rails? 在Rails上运行npm软件包的最佳方法是什么? - What's the best way to run npm packages on Rails? 在Rails应用程序中运行异步作业的最佳方法是什么? - What is the best way to run asynchronous jobs in a Rails application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM