Puppet Function: batch_escape

Defined in:
vendor_modules/stdlib/lib/puppet/functions/batch_escape.rb
Function type:
Ruby 4.x API

Summary

Escapes a string so that it can be safely used in a batch shell command line.

Overview

batch_escape(Any $string)Any

>* Note:* that the resulting string should be used unquoted and is not intended for use in double quotes nor in single quotes.

Parameters:

  • string (Any)

    The string to escape

Returns:

  • (Any)

    An escaped string that can be safely used in a batch command line.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'vendor_modules/stdlib/lib/puppet/functions/batch_escape.rb', line 8

Puppet::Functions.create_function(:batch_escape) do
  # @param string
  #   The string to escape
  #
  # @return
  #   An escaped string that can be safely used in a batch command line.
  dispatch :batch_escape do
    param 'Any', :string
  end

  def batch_escape(string)
    result = ''

    string.to_s.chars.each do |char|
      result += case char
                when '"' then '""'
                when '$', '\\' then "\\#{char}"
                else char
                end
    end

    %("#{result}")
  end
end