虽然我的1.8.7版本似乎有一个Shellwords::shellescape的后端口版本,但我知道该方法是1.9的特性,在1.8的早期版本中肯定不支持。有谁知道我在哪里可以找到一个健壮的独立的Ruby转义的Bourne-shell命令实现,无论是Gem形式的还是一个代码片段?
发布于 2009-08-24 14:02:52
我最终选择了Escape gem,它有一个额外的特性,默认情况下使用引号,只有在必要时才使用反斜杠转义。
发布于 2009-08-24 00:47:53
你也可以把你想要的东西复制到shellwords.rb的subversion仓库(即GPLv2‘d)中:
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
endhttps://stackoverflow.com/questions/1306680
复制相似问题