gpt4 book ai didi

ruby - 在 Ruby 中运行缺少 shebang 的 shell 脚本

转载 作者:太空宇宙 更新时间:2023-11-03 16:51:58 24 4
gpt4 key购买 nike

我在 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”中找到它的典故。然而,回复驳斥了换行符改变命令语义的想法,它似乎确实如此。

有什么想法吗?

最佳答案

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

~/echo 'echo "foobar"' > hello.sh~/猫你好.sh echo “foobar”~/IRB1.9.3p194 :001 > ./hello.sh => "foobar\n"1.9.3p194 :002 > ./hello.sh\n => "foobar\n"

运行 Ruby 1.9.3、zshell、Mac OS

关于ruby - 在 Ruby 中运行缺少 shebang 的 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19283256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com