【问题标题】:Augment ruby $LOAD_PATH with ENV['PATH'] still fails to find backtick command使用 ENV['PATH'] 扩展 ruby​​ $LOAD_PATH 仍然无法找到反引号命令
【发布时间】:2013-09-01 07:44:16
【问题描述】:

我正在尝试从 ruby​​ 脚本 (Ruby 1.9.3) 中运行一个名为 pdfgrep 的程序,该脚本是从 Windows 7 上的 Git Bash (MINGW32) shell 运行的。对 pdfgrep 的调用在反引号,我想得到结果并使用 ruby​​ 方法清理它。

在查看this answerthis answera related SO question 之后,我想我应该将ENV['PATH'] 变量从Windows 样式路径转换为unix 路径,这是我的shell 中的默认路径。

(第 3 行)所以我用简单的puts 测试了$LOAD_PATH 的内容,这似乎有效。

script.rb

#!/usr/bin/env ruby

ENV['PATH'].split('\\').join(File::SEPARATOR).split(';').inject($LOAD_PATH) {|lp,v| lp << v }

# The intention is to replace puts with a variable assignment
puts  `pdfgrep -i "(issued|Total)" *.pdf` 

运行时我得到的错误是:

./script.rb:6:in ``': No such file or directory - pdfgrep -i "(issued|Total)" *.pdf (Errno::ENOENT)

当我提供完整路径时 - c:/Users/cjross/bin/pdfgrep -i [etc...] - 没有错误消息。它仍然没有输出我所期望的,但它不会引发错误。

基本上,我希望能够从 ruby​​ 调用我的 shell $PATH 中的任何程序。任何建议将不胜感激。

编辑 感谢@konsolebox 纠正我不正确的引用。不幸的是仍然有路径问题。

【问题讨论】:

    标签: ruby bash shell path git-bash


    【解决方案1】:
    puts `pdfgrep -i '(issued|Total)' "*.pdf"` 
    

    我认为应该作为

    puts `pdfgrep -i "(issued|Total)" *.pdf`
    

    【讨论】:

    • 不幸的是,它仍然引发了关于 No such file or directory 的错误,但是您对我的报价的修改至少让我得到了预期的输出(尽管有 pdfgrep 的完整路径)。谢谢!我会将其包含在我的问题中。
    猜你喜欢
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    相关资源
    最近更新 更多