如何将 Ruby 脚本变成 bash 命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28018591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make a Ruby script into a bash command?
提问by Doug Smith
I have a Ruby file, and I run it as ruby file.rb "parameters"
. I prefer to run it as regtask parameters
without having to include ruby
and the filename every time. I want it to be on the same level as ls
. How would I accomplish this?
我有一个 Ruby 文件,我将它作为ruby file.rb "parameters"
. 我更喜欢运行它,regtask parameters
而不必ruby
每次都包含文件名。我希望它与ls
. 我将如何做到这一点?
回答by Amadan
Edit your file, make sure this is the first line, so your system knows how to execute your file:
编辑您的文件,确保这是第一行,以便您的系统知道如何执行您的文件:
#!/usr/bin/env ruby
Next, change the file's permissions to make it executable:
接下来,更改文件的权限以使其可执行:
chmod a+x file.rb
And finally, rename it and move it somewhere where it will be executed without having to write its full path:
最后,重命名它并将其移动到将要执行的地方,而无需编写其完整路径:
mkdir -p ~/bin
mv file.rb ~/bin/regtask
(Most systems will automatically add ~/bin
to PATH
if it exists; if not, you will have to add it to PATH
yourself in your startup files.)
(大多数系统将自动添加~/bin
到PATH
,如果它存在;如果没有,则必须将其添加到PATH
自己在你的启动文件。)
回答by contact411
This should help.. Please let me know if you run into any issues.
这应该会有所帮助.. 如果您遇到任何问题,请告诉我。
http://commandercoriander.net/blog/2013/02/16/making-a-ruby-script-executable/
http://commandercoriander.net/blog/2013/02/16/making-a-ruby-script-executable/
Making a Ruby Script Executable
使 Ruby 脚本可执行
It's common knowledge in the *nix community, but for many new developers turning a Ruby script into an executable command line program is akin to magic. While there are other references on the internet, for the post here, I will briefly explain how to go from running a Ruby script by invoking Ruby directly, to running the script by its name alone.
这是 *nix 社区的常识,但对于许多新开发人员来说,将 Ruby 脚本转换为可执行的命令行程序就像魔术一样。虽然互联网上还有其他参考资料,但对于此处的帖子,我将简要说明如何从通过直接调用 Ruby 来运行 Ruby 脚本,到仅通过其名称运行脚本。
We will start by assuming we have a simple Ruby script which prints "hello" on the command line. Our script's name will be greeter.rb
. The file holds one line of Ruby code:
我们首先假设我们有一个简单的 Ruby 脚本,它在命令行上打印“hello”。我们的脚本名称将是greeter.rb
. 该文件包含一行 Ruby 代码:
puts "Hello!"`
To run the script, we must type ruby greeter.rb
. Wouldn't it be nice to just type greeter
instead and still get the script to run? Yes, it would.
要运行脚本,我们必须输入ruby greeter.rb
. 只是键入greeter
而不是让脚本运行不是很好吗?是的,会的。
First, we need to tell Bash what to do with our file since we won't be passing the script to Ruby directly. To do that, we add the following to the very top of our script:
首先,我们需要告诉 Bash 如何处理我们的文件,因为我们不会直接将脚本传递给 Ruby。为此,我们将以下内容添加到脚本的最顶部:
#!/usr/bin/env ruby
puts "Hello!"
The first line is a Bash directive and basically tells Bash what program to run our file with by asking for the current configured version of Ruby as specified by the env
command. For more on how env
works, try typing man env
into the command line.
第一行是 Bash 指令,通过询问env
命令指定的当前配置的 Ruby 版本,基本上告诉 Bash 用什么程序运行我们的文件。有关env
工作原理的更多信息,请尝试man env
在命令行中输入。
Second, we need to make our script executable, which requires changing the file permissions. If the concept of file permissions is new, read about it here. Bascially, files have three types of permissions. They can be read, written, and executed. Most files typically start out as only having read and write access. Since we want to execute our script, we're going to have to grant it execute permissions.
其次,我们需要使我们的脚本可执行,这需要更改文件权限。如果文件权限的概念是新的,请在此处阅读。基本上,文件具有三种类型的权限。它们可以被读取、写入和执行。大多数文件开始时通常只有读写访问权限。由于我们想要执行我们的脚本,我们将不得不授予它执行权限。
Doing that is just a simple Bash command. On the command line, navigate to the directory holding the greeter.rb
file. Now, to check the permissions, run:
这样做只是一个简单的 Bash 命令。在命令行上,导航到保存greeter.rb
文件的目录。现在,要检查权限,请运行:
ls -l greeter.rb
The output will look something like this:
输出将如下所示:
-rw-r--r-- 1 username staff 13 Feb 16 21:10 greeter.rb
Your own username will show up in the place of username
, and the creation date will naturally be different, but otherwise the output will be almost identical. The first part of the line is the revelant part. The letters r
and w
specify read and write permissions.
您自己的用户名将出现在 的位置username
,创建日期自然会有所不同,否则输出将几乎相同。该行的第一部分是启示部分。字母r
并w
指定读写权限。
We're going to add execute permissions which will appear as an x in that line. To add execute permissions, run the following command.
我们将添加执行权限,该权限将在该行中显示为 x。要添加执行权限,请运行以下命令。
chmod 755 greeter.rb
Now, if you check the file permissions again with ls -l greeter.rb
, the output should be a little different.
现在,如果您再次使用 来检查文件权限ls -l greeter.rb
,输出应该会有所不同。
-rwxr-xr-x 1 username staff 13 Feb 16 21:20 greeter.rb
The presence of x
indicates that the file can be run directly without calling Ruby first. The following command should get our file to say "hello."
的存在x
表示该文件可以直接运行,无需先调用Ruby。以下命令应该让我们的文件说“你好”。
./greeter.rb
Almost there. Now, we just need to get rid of the prefix ./
, which tells Bash where to look for greeter.rb
, i.e., in the current directory. Before we complete this last step, though, let's rename our file to just greeter
.
差不多好了。现在,我们只需要去掉前缀./
,它告诉 Bash 在哪里寻找greeter.rb
,即在当前目录中。不过,在我们完成最后一步之前,让我们将文件重命名为greeter
.
mv greeter.rb greeter
Now, for the last step. Everytime we call a Bash program, e.g., ls
, chmod
, mv
, etc., Bash searches through a predefined list of folders looking for those programs. This is called the path. To see what the path is set to on your computer, try:
现在,进行最后一步。每次我们调用bash程序,例如ls
,chmod
,mv
等,通过寻找那些程序文件夹的预定义列表猛砸搜索。这称为路径。要查看您计算机上的路径设置,请尝试:
echo "$PATH"
The output should be a long string of various system-critical folders. We need to put our application into one of these folders. Traditionally, it's best to leave folders like /usr/bin/
and /bin/
alone. Instead, any kind of user additions should be placed in /usr/local/bin/
. If that folder doesn't exist, create it with:
输出应该是一长串各种系统关键文件夹。我们需要将我们的应用程序放入这些文件夹之一。传统上,这是最好的离开像文件夹/usr/bin/
和/bin/
孤独。相反,任何类型的用户添加都应该放在/usr/local/bin/
. 如果该文件夹不存在,请使用以下命令创建它:
mkdir -p /usr/local/bin/
Now, we can either move our greeter into that folder, or leave the application where it is and just create a softlink (or an alias in OS X terms) within the /usr/local/bin/
folder. To create an alias, we'll use the ln
command. From the directory where greeter
lives, type:
现在,我们可以将我们的欢迎程序移动到该文件夹中,也可以将应用程序保留在原处,然后在该/usr/local/bin/
文件夹中创建一个软链接(或 OS X 术语中的别名)。要创建别名,我们将使用该ln
命令。从所在的目录中greeter
,键入:
ln -s $PWD/greeter /usr/local/bin/
Note that the $PWD
variable will expand to an absolute path to our greeter script. Now, we're done and we can simply type greeter
to invoke our Ruby script!
ln -s $PWD/greeter /usr/local/bin/
请注意,该$PWD
变量将扩展为我们的欢迎脚本的绝对路径。现在,我们已经完成了,我们可以简单地输入greeter
来调用我们的 Ruby 脚本!
As a footnote, if any of the above Bash commands seem confusing, trying looking up their man page by typing man <command>
.
作为脚注,如果上述 Bash 命令中的任何一个看起来令人困惑,请尝试通过键入man <command>
.