ruby - 使用 Ruby 执行命令行时如何转义竖线?

标签 ruby windows cmd

我在 Windows 中有这个命令行输入:

dir /b | ruby -ne 'f=$_.chomp;File.read(f).each_line{|line| print f if line =~ /helloworld/};'

这一行是查找当前目录下所有包含helloworld的文件。 (我知道还有很多其他方法可以做到这一点,但这篇文章并不关注这一点)

我的问题是:

此行给出错误提示 line is not a valid command。然后我意识到 | 在 Ruby 中作为 block 变量的大括号是 cmd 中的管道符号。我试图将整行更改为双引号并转义 |签名但没有成功。如何转义竖线 |?

请不要回答其他方法来完成任务,例如使用其他工具,如 grep、find、sed 或 awk,或将 block 更改为 for 循环。

最佳答案

我找到了 this microsoft.com page关于管道和特殊字符的说明如下:

The ampersand (&), pipe (|), and parentheses ( ) are special characters that must be preceded by the escape character (^) or quotation marks when you pass them as arguments.

关于ruby - 使用 Ruby 执行命令行时如何转义竖线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12644837/

相关文章:

ruby - 如何将 --profile 开关与 aws-rb(Ruby gem aws-sdk 的一部分)一起使用?

java - 在 Windows cmd 中使用 JAR 文件别名

windows - cmd 批量调用历史命令

ruby-on-rails - 使用现有的 SQL Server 数据库和 Ruby on Rails

c# - 编辑 M4V 视频元数据

ruby - better_errors 要求 Ruby 版本 >= 2.0.0

c++ - Mingw x64 Windows : plugin needed to handle lto object

windows - Windows drag'n'drop 在哪里存储拖动的项目数据?

java - JAR 文件如何 self 删除?

python - 使用 python cmd 模块实现 unix 管道?