Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.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
Vargrantfile-Ruby-意外的文件结尾_Ruby_Vagrant_Vagrantfile - Fatal编程技术网

Vargrantfile-Ruby-意外的文件结尾

Vargrantfile-Ruby-意外的文件结尾,ruby,vagrant,vagrantfile,Ruby,Vagrant,Vagrantfile,我对ruby是新手。我试图修改现有的ruby语法的Vargrantfile。 我在下面 def has_program(program) ENV['PATH'].split(File::PATH_SEPARATOR).any? do |directory| File.executable?(File.join(directory, program.to_s)) end end is_exist = has_program("some-command") puts is_exi

我对ruby是新手。我试图修改现有的ruby语法的Vargrantfile。 我在下面

def has_program(program)
  ENV['PATH'].split(File::PATH_SEPARATOR).any? do |directory|
    File.executable?(File.join(directory, program.to_s))
  end
end 

is_exist = has_program("some-command")
puts is_exist
$my_script = %{
if is_exist == false
  if ! some-command status; then
    #Do some staff
  fi
end
# do some staff
}

Vagrant.configure("2") do |config|

   node.vm.provision "shell", inline: $my_script
end
然后,在运行Wagrant up--provision时,我发现以下错误

syntax error: unexpected end of file
你能告诉我我犯了什么错误吗

关于,,
-M-

这是一个语法错误,但在Ruby代码中并非如此。它是从Ruby脚本执行的shell代码中的未完成语句

如果未关闭块,则可能发生这种情况。解析器希望找到它的结尾,但却遇到了脚本的结尾

让我们看看执行shell命令的部分

$my_script = %{
if is_exist == false
  if ! some-command status; then
    #Do some staff
  fi
end
# do some staff
}
现在,让我们剥去它周围的红宝石部分。作业
$my_script=
仍然是Ruby代码。大括号中的部分是中的字符串文字,稍后将使用Vagrant的。。。但是,在结束字符串文字之前,似乎要切换回Ruby语法

解释器解析为shell脚本的是以下部分:

if is_exist == false
  if ! some-command status; then
    #Do some staff
  fi
end
# do some staff
注意,整个外部
if
表达式使用Ruby的
if
语法。它不是有效的shell命令,因此出现错误


我不确定在您的例子中这个表达式的语义是什么,但是您需要将它转换为shell
if
,或者使用
inline
选项将它移到传递给Vagrant的字符串文本之外。另一方面,里面的逻辑似乎很奇怪。如果
有程序(“some command”)
返回false,则调用的是
some命令。但这是另一个故事:)

非常感谢您的解释,我现在已经修好了
if is_exist==false
was用于测试。