Class Index [+]

Quicksearch

Mail::ShellEscape

Public Class Methods

escape_for_shell(str) click to toggle source

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

Note that a resulted string should be used unquoted and is not intended for use in double quotes nor in single quotes.

  open("| grep #{Shellwords.escape(pattern)} file") { |pipe|
    # ...
  }

+String#shellescape+ is a shorthand for this function.

  open("| grep #{pattern.shellescape} file") { |pipe|
    # ...
  }
    # File lib/mail/core_extensions/shell_escape.rb, line 25
25:     def escape_for_shell(str)
26:       # An empty argument will be skipped, so return empty quotes.
27:       return "''" if str.empty?
28: 
29:       str = str.dup
30: 
31:       # Process as a single byte sequence because not all shell
32:       # implementations are multibyte aware.
33:       str.gsub!(/([^A-Za-z0-9_\s\+\-.,:\/@\n])/, "\\\\\\1")
34: 
35:       # A LF cannot be escaped with a backslash because a backslash + LF
36:       # combo is regarded as line continuation and simply ignored.
37:       str.gsub!(/\n/, "'\n'")
38: 
39:       return str
40:     end

Private Instance Methods

escape_for_shell(str) click to toggle source

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

Note that a resulted string should be used unquoted and is not intended for use in double quotes nor in single quotes.

  open("| grep #{Shellwords.escape(pattern)} file") { |pipe|
    # ...
  }

+String#shellescape+ is a shorthand for this function.

  open("| grep #{pattern.shellescape} file") { |pipe|
    # ...
  }
    # File lib/mail/core_extensions/shell_escape.rb, line 25
25:     def escape_for_shell(str)
26:       # An empty argument will be skipped, so return empty quotes.
27:       return "''" if str.empty?
28: 
29:       str = str.dup
30: 
31:       # Process as a single byte sequence because not all shell
32:       # implementations are multibyte aware.
33:       str.gsub!(/([^A-Za-z0-9_\s\+\-.,:\/@\n])/, "\\\\\\1")
34: 
35:       # A LF cannot be escaped with a backslash because a backslash + LF
36:       # combo is regarded as line continuation and simply ignored.
37:       str.gsub!(/\n/, "'\n'")
38: 
39:       return str
40:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.