简体   繁体   English

如何将带有双斜杠的属性值传递给模板

[英]How to pass attribute value with double slash to a template

I am trying to develop a cookbook which is going to support both Windows and Linux platform. 我正在尝试开发将同时支持Windows和Linux平台的食谱。 I am having some issue while working with a template. 使用模板时出现问题。 So, basically I am trying to put a shell script which will have a export command with a path. 因此,基本上,我正在尝试放置一个shell脚本,该脚本将具有带有路径的export命令。 But I am not able to get the exact value on the shell script on the windows. 但是我无法在Windows的Shell脚本上获得确切的值。

Attribute: 属性:

root_path = value_for_platform(
  'windows' => { 'default' => "D:\\" },
  'default' => '/opt'
)
xyz_path = value_for_platform(
  'windows' => { 'default' => "#{root_path}xyz_app\\tool\\bin\\xyz.bat" },
  'default' => "#{root_path}/xyz_app//tool//bin//xyz"
)

Recipe: 食谱:

template File.join(root_path, '/tmp/xyz_path.sh') do
  source 'xyz_path.sh.erb'
  mode '0755'
  variables({
    :xyz_path => xyz_path
  })
end

Template 模板

#!/bin/bash

export xyz_path = <%= @xyz_path %>

Expectation on Windows 在Windows上的期望

#!/bin/bash

export xyz_path = D:\\xyz_app\\tool\\bin\\xyz.bat

Reality on Windows Windows上的现实

#!/bin/bash

export xyz_path = D:\xyz_app\tool\bin\xyz.bat

Use 4 backslashes to get what you desire . 使用4个反斜杠即可获得所需的内容。

root_path = value_for_platform(
  'windows' => { 'default' => "D:\\\\" },
  'default' => '/opt'
)
xyz_path = value_for_platform(
  'windows' => { 'default' => "#{root_path}xyz_app\\\\tool\\\\bin\\\\xyz.bat" },
  'default' => "#{root_path}/xyz_app//tool//bin//xyz"
)

Since the backslash is the escape character the first backslash escapes the second one so you need to repeat this twice in order to get two backslashes in the output 由于反斜杠是转义字符,因此第一个反斜杠将转义第二个斜杠,因此您需要重复两次以在输出中获得两个反斜杠。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM