为什么我的Ruby脚本无法执行? [英] Why won't my Ruby script execute?

查看:89
本文介绍了为什么我的Ruby脚本无法执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我制作了一个简单的ruby脚本

So, I made a simply ruby script,

#!/usr/bin/env ruby

puts "Hello!"

当我尝试在终端中运行它时,它不会显示"Hello!".屏幕上.我尝试输入 chmod + x test.rb ( test.rb 是我的文件的名称).当我运行它时,它不会给我错误,只是不显示"Hello!".任何帮助都感激不尽.我到处都在寻找可能的答案,但到目前为止我什么都没发现.

When I try to run it in terminal it doesn't put "Hello!" on the screen. I have tried entering chmod +x test.rb (test.rb is the name of my file). When I run it, it doesn't give me an error, it just doesn't display "Hello!". Any help will be much appreciated. I have looked everywhere for a possible answer, and I have found nothing so far.

推荐答案

我猜您正在尝试像 test 这样运行它:

I'd guess that you're trying to run it as just test like this:

$ test

但是 test 是内置的bash 命令,它不会产生任何输出,它只是设置一个返回值.如果您正确运行脚本,则:

But test is a bash builtin command that doesn't produce any output, it just sets a return value. If you run your script properly:

$ ./test.rb

然后您会看到一些东西.请注意显式的 ./路径,当前目录在 PATH 中很少(希望永远不在),因此您需要说出 ./来运行当前目录中的某些内容(除非您当然位于/bin /usr/bin 等).

then you'll see something. Note the explicit ./ path, the current directory is rarely (and hopefully never) in your PATH so you need to say ./ to run something in the current directory (unless of course you're in /bin, /usr/bin, etc.).

在注释中,您说脚本中有一些 Ctrl + M 字符:

In the comments you say that there are some Ctrl+M characters in your script:

$ cat -e test.rb
#!/usr/bin/env ruby^M^Mputs "Hello!"

在该 cat -e 输出中没有看到任何 $ ,因此您没有任何实际的行尾标记,只是一些回车符-返回字符(即 ^ M ).单个CR是旧的MacOS行尾,Windows使用CR-LF对,而Unix(包括OSX)仅使用单个LF来标记文本行的结尾.由于您没有任何停产声明,因此外壳程序只会看到一行类似于:

I don't see any $s in that cat -e output so you don't have any actual end-of-line markers, just some carriage-return characters (that's the ^M). A single CR is an old MacOS end-of-line, Windows uses a CR-LF pair, and Unix (including OSX) uses just a single LF to mark the end of a line of text. Since you don't have any EOLs, the shell just sees a single line that looks like:

#!/usr/bin/env ruby ...

在没有运行 ruby​​ 的实际脚本的情况下,shell只会看到shebang注释,而没有其他内容.结果是,在运行脚本时,没有任何明显的事情发生.修复您的EOL,脚本将开始正常工作.您可能还需要查看编辑器的设置,以便它开始编写适当的EOL.

without an actual script for ruby to run, the shell just sees the shebang comment and nothing else. The result is that nothing noticeable happens when you run your script. Fix your EOLs and your script will start working sensibly. You might also want to look at your editor's settings so that it starts writing proper EOLs.

这篇关于为什么我的Ruby脚本无法执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆