Module: Bulldog::Util::Shellwords
- Defined in:
- lib/bulldog/util.rb
Class Method Summary collapse
-
.escape(str) ⇒ Object
Escapes a string so that it can be safely used in a Bourne shell command line.
-
.join(array) ⇒ Object
Builds a command line string from an argument list
array
joining all elements escaped for Bourne shell and separated by a space.
Class Method Details
.escape(str) ⇒ Object
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|
# ...
}
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/bulldog/util.rb', line 43 def escape(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 |
.join(array) ⇒ Object
Builds a command line string from an argument list array
joining all elements escaped for Bourne shell and separated by a space.
open('|' + Shellwords.join(['grep', pattern, *files])) { |pipe|
# ...
}
70 71 72 |
# File 'lib/bulldog/util.rb', line 70 def join(array) array.map { |arg| escape(arg) }.join(' ') end |