Module: Shellwords
- Defined in:
- lib/mail/core_extensions/shellwords.rb
Overview
The following is imported from ruby 1.9.2 shellwords.rb
Class Method Summary collapse
-
.shellescape(str) ⇒ Object
(also: escape)
Escapes a string so that it can be safely used in a Bourne shell command line.
Class Method Details
.shellescape(str) ⇒ Object Also known as: escape
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|
# ...
}
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mail/core_extensions/shellwords.rb', line 22 def shellescape(str) # An empty argument will be skipped, so return empty quotes. return "''" if str.empty? str = str.dup # Process as a single byte sequence because not all shell # implementations are multibyte aware. str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1") # A LF cannot be escaped with a backslash because a backslash + LF # combo is regarded as line continuation and simply ignored. str.gsub!(/\n/, "'\n'") return str end |