本文简要介绍ruby语言中 Shellwords.shellescape
的用法。
用法
shellescape(str)
也别名为:escape
转义字符串,以便可以在 Bourne shell 命令行中安全地使用它。 str
可以是响应 to_s
的非字符串对象。
请注意,结果字符串应该不带引号使用,并且不打算在双引号或单引号中使用。
argv = Shellwords.escape("It's better to give than to receive")
argv #=> "It\\'s\\ better\\ to\\ give\\ than\\ to\\ receive"
String#shellescape
是此函数的简写。
argv = "It's better to give than to receive".shellescape
argv #=> "It\\'s\\ better\\ to\\ give\\ than\\ to\\ receive"
# Search files in lib for method definitions
pattern = "^[ \t]*def "
open("| grep -Ern -e #{pattern.shellescape} lib") { |grep|
grep.each_line { |line|
file, lineno, matched_line = line.split(':', 3)
# ...
}
}
调用者有责任为使用该字符串的 shell 环境以正确的编码对字符串进行编码。
多字节字符被视为多字节字符,而不是字节。
如果 str
的长度为零,则返回空引用的 String
。
相关用法
- Ruby Shellwords.shellsplit用法及代码示例
- Ruby Shellwords.shelljoin用法及代码示例
- Ruby Shellwords模块用法及代码示例
- Ruby Symbol capitalize用法及代码示例
- Ruby SizedQueue clear()用法及代码示例
- Ruby Spotter.spot_op_asgn2_for_name用法及代码示例
- Ruby StringScanner skip_until用法及代码示例
- Ruby StringScanner search_full用法及代码示例
- Ruby String.match?用法及代码示例
- Ruby StringScanner.beginning_of_line?用法及代码示例
- Ruby Stat.stat <=>用法及代码示例
- Ruby Symbol.to_proc用法及代码示例
- Ruby Symbol.end_with?用法及代码示例
- Ruby StringScanner restsize用法及代码示例
- Ruby Spotter.spot_opcall_for_name用法及代码示例
- Ruby Set flatten()用法及代码示例
- Ruby SpecificationProvider.requirement_satisfied_by?用法及代码示例
- Ruby Set.replace用法及代码示例
- Ruby String delete!用法及代码示例
- Ruby String chop!用法及代码示例
- Ruby SizedQueue push()用法及代码示例
- Ruby Specification.file_name用法及代码示例
- Ruby SimpleDelegator.__setobj__用法及代码示例
- Ruby String.unpack用法及代码示例
- Ruby Stat.world_writable?用法及代码示例
注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Shellwords.shellescape。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。