Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Ruby脚本运行Linux系统命令_Ruby_Linux_System - Fatal编程技术网

从Ruby脚本运行Linux系统命令

从Ruby脚本运行Linux系统命令,ruby,linux,system,Ruby,Linux,System,我使用以下Ruby脚本创建了一个Debian软件包,效果很好: #!/usr/bin/ruby dest = "#{File.dirname(__FILE__)}/../build" package = "foo" [ "cd #{dest} && tar czvf data.tar.gz bin console data.sql etc filter install.rb", "cd #{dest} && tar czvf control.tar.gz cont

我使用以下Ruby脚本创建了一个Debian软件包,效果很好:

#!/usr/bin/ruby dest = "#{File.dirname(__FILE__)}/../build" package = "foo" [ "cd #{dest} && tar czvf data.tar.gz bin console data.sql etc filter install.rb", "cd #{dest} && tar czvf control.tar.gz control", "cd #{dest} && echo 2.0 > debian-binary", "cd #{dest} && ar -cr #{package}.deb debian-binary control.tar.gz data.tar.gz", "cd #{dest} && mv #{package}.deb ..", "cd #{dest} && rm data.tar.gz control.tar.gz", ].each do |command| puts command system(command) end 在Ruby中有没有一种方法可以让我不使用每个命令的cd{dest}&&部分

Dir.chdir(dest) do
  # code that shall be executed while in the dest directory
end
当使用块调用Dir.chdir时,它将更改为给定的目录,执行该块,然后再更改回原来的目录

您也可以在不使用块的情况下使用它,在这种情况下,它将永远不会更改回原来的状态。

是。使用Dir.chdir:


含糖的工作得很有魅力。谢谢
#!/usr/bin/ruby

  dest = "#{File.dirname(__FILE__)}/../build"
  package = "foo"

  Dir.chdir dest
  [
    "tar czvf data.tar.gz bin console data.sql etc filter install.rb",
    "tar czvf control.tar.gz control",
    "echo 2.0 > debian-binary",
    "ar -cr #{package}.deb debian-binary control.tar.gz data.tar.gz",
    "mv #{package}.deb ..",
    "rm data.tar.gz control.tar.gz",
  ].each do |command|
    puts command
    system(command)
  end