如何将Ruby脚本转换为bash命令?

时间:2021-12-02 01:10:47

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“参数”运行。我更喜欢将它作为regtask参数运行,而不必每次都包含ruby和文件名。我希望它与ls处于同一水平。我怎么做到这一点?

2 个解决方案

#1


12  

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。)

#2


2  

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.

您自己的用户名将显示在用户名的位置,创建日期自然会有所不同,但输出几乎相同。该系列的第一部分是重要部分。字母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等,Bash搜索预定义的文件夹列表,寻找那些程序。这称为路径。要查看计算机上的路径设置,请尝试:

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:

现在,我们可以将我们的greeter移动到该文件夹​​中,或者将应用程序保留在原来的位置,只需在/ 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脚本的绝对路径。现在,我们已经完成了,我们可以简单地输入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 来查找其手册页。

#1


12  

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。)

#2


2  

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.

您自己的用户名将显示在用户名的位置,创建日期自然会有所不同,但输出几乎相同。该系列的第一部分是重要部分。字母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等,Bash搜索预定义的文件夹列表,寻找那些程序。这称为路径。要查看计算机上的路径设置,请尝试:

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:

现在,我们可以将我们的greeter移动到该文件夹​​中,或者将应用程序保留在原来的位置,只需在/ 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脚本的绝对路径。现在,我们已经完成了,我们可以简单地输入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 来查找其手册页。