[英]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()
函数返回true
或false
具体取决于命令是否成功。 Additionally, $?
另外,
$?
contains an error status. 包含错误状态。
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进行验证。 $?
$?
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.