简体   繁体   English

Ruby on Rails脚本控制台

[英]Ruby on Rails script console

I wasn't able to run ./script/console previously, and it used to throw an error since my script console file had included #!/usr/bin/env ruby19 . 我之前无法运行./script/console ,因为我的script console文件包含了#!/usr/bin/env ruby19 ,所以它曾经抛出错误。 After doing hit and trial I fixed this error by replacing #!/usr/bin/env ruby19 with #!/usr/bin/env ruby . 做命中和审判后,我通过更换固定此错误#!/usr/bin/env ruby19#!/usr/bin/env ruby

What does the above line do? 以上几行有什么作用?

Versions: 版本:

  • Ruby: 1.9.2-p180 Ruby:1.9.2-p180
  • Ruby on Rails: 2.3.5 Ruby on Rails:2.3.5

The #! #! (hash bang) in the first line of a text file tells the program loader in most *nix systems to invoke the program that is specified next (in this case, /usr/bin/env ) with any params supplied (in this case, ruby ). (hash bang)在文本文件的第一行中告诉大多数* nix系统中的程序加载器调用下一个指定的程序(在本例中为/usr/bin/env ),并提供任何params(在这种情况下, ruby )。

/usr/bin/env is just a portable way of looking in your environment for program named in the first argument. /usr/bin/env只是一种可移植的方式,可以在您的环境中查找第一个参数中指定的程序。 Here it is the Ruby interpreter. 这是Ruby解释器。 If the Ruby interpreter is in your PATH, env will find it and run it using the rest of the file as input. 如果Ruby解释器在你的PATH中,env会找到它并使用文件的其余部分作为输入来运行它。

You probably didn't have a program named ruby19 in your PATH, so you'd get the error. 您可能在PATH中没有名为ruby19的程序,因此您将收到错误消息。 You do have a program named ruby , so that works. 你有一个名为ruby的程序,这样才行。

The shebang line in a Unix script is supposed to specify a full path, so this: Unix脚本中的shebang行应该指定一个完整路径,所以这个:

#!/usr/local/bin/ruby

is valid but this is not: 是有效的,但这不是:

#!ruby

The problem with the first form is that, well, you need to use a full path but the actual path won't be the same on all systems. 第一种形式的问题是,您需要使用完整路径,但实际路径在所有系统上都不一样。 The env utility is often used to allow a script to search the PATH environment variable for the appropriate interpreter, env should always be in /usr/bin/env so you can safely use that as a full path and then let env search the PATH for the named interpreter. env实用程序通常用于允许脚本在PATH环境变量中搜索适当的解释器, env应始终位于/usr/bin/env这样您就可以安全地将其用作完整路径,然后让env搜索PATH命名的口译员。

From the fine manual : 精细手册

env [-i] [name=value]... [utility [argument...]]

The env utility shall obtain the current environment, modify it according to its arguments, then invoke the utility named by the utility operand with the modified environment. env实用程序应获取当前环境,根据其参数进行修改,然后使用修改后的环境调用实用程序操作数命名的实用程序。

That isn't terribly helpful in your case but I figured I should include it anyway. 这在你的情况下并不是非常有用,但我认为无论如何我应该包括它。 The shebang use of env is a bit of a hack that doesn't use the intended behavior of env . 使用env的shebang是一种不使用env的预期行为的hack。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM