【问题标题】:Running shell scripts missing shebang within Ruby在 Ruby 中运行缺少 shebang 的 shell 脚本
【发布时间】:2013-10-09 21:39:41
【问题描述】:

我在 Ruby 中发现了一个我无法解释的有趣问题。

我试图运行一个具有执行权限的 shell 脚本,但 shell 脚本意外遗漏了顶部的 shebang (#!),它描述了如何运行所述脚本。

如果我尝试使用反引号 (`) 在 irb 中运行脚本,它会显示:

>> `./hello.sh`
(irb):3: command not found: ./hello.sh

很公平。但是,如果我改为输入

>> `./hello.sh\n`
=> "hello\n"

它出奇地有效。请注意,%x{...} 的行为与上述一致(考虑到它们的等价性,这并不奇怪),但system() 不起作用,无论是否换行(我可以)。此外,毫不奇怪,将适当的 #! 添加回 shell 脚本 (hello.sh) 会使一切正常。

我试图寻找答案,但我只能在“Command not found when I call `./myshell` in ruby”中找到一个暗示。然而,回复驳斥了换行符改变命令语义的想法,这似乎是这样做的。

有什么想法吗?

【问题讨论】:

    标签: ruby shell


    【解决方案1】:

    我无法重现这个问题,对我来说,这一切正常:

    ~/ echo 'echo "foobar"' > hello.sh ~/猫你好.sh 回声“foobar” 〜/ irb 1.9.3p194:001 > ./hello.sh => "foobar\n" 1.9.3p194:002 > ./hello.sh\n => "foobar\n"

    运行 Ruby 1.9.3、zshell、Mac OS

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-10
      • 2013-07-29
      • 1970-01-01
      • 2012-05-11
      • 2016-07-18
      • 1970-01-01
      • 2012-10-06
      相关资源
      最近更新 更多