简体   繁体   English

如何将Ruby脚本制作为bash命令?

[英]How do I make a Ruby script into a bash command?

I have a Ruby file, and I run it as ruby file.rb "parameters" . 我有一个Ruby文件,并将其作为ruby file.rb "parameters" I prefer to run it as regtask parameters without having to include ruby and the filename every time. 我更喜欢将其作为regtask parameters运行,而不必每次都包含ruby和文件名。 I want it to be on the same level as ls . 我希望它与ls处于同一级别。 How would I accomplish this? 我将如何完成?

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

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. 这是* nix社区中的常识,但是对于许多新开发人员而言,将Ruby脚本转换为可执行的命令行程序就像魔术一样。 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. 尽管Internet上还有其他参考,但对于此处的帖子,我将简要说明如何从直接调用Ruby运行Ruby脚本到仅按其名称运行脚本。

We will start by assuming we have a simple Ruby script which prints "hello" on the command line. 我们首先假设我们有一个简单的Ruby脚本,该脚本在命令行上显示“ hello”。 Our script's name will be greeter.rb . 我们的脚本名称将为greeter.rb The file holds one line of Ruby code: 该文件包含一行Ruby代码:

puts "Hello!"`

To run the script, we must type ruby greeter.rb . 要运行脚本,我们必须输入ruby greeter.rb Wouldn't it be nice to just type greeter instead and still get the script to run? 只输入greeter并仍然运行脚本会不会很好呢? Yes, it would. 是的,会的。

First, we need to tell Bash what to do with our file since we won't be passing the script to Ruby directly. 首先,我们需要告诉Bash如何处理文件,因为我们不会直接将脚本传递给Ruby。 To do that, we add the following to the very top of our script: 为此,我们将以下内容添加到脚本的最上方:

#!/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. 第一行是Bash指令,它通过询问env命令指定的Ruby当前配置版本,基本上告诉Bash运行文件的程序。 For more on how env works, try typing man env into the command line. 有关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. 这样做只是一个简单的Bash命令。 On the command line, navigate to the directory holding the greeter.rb file. 在命令行上,导航到包含greeter.rb文件的目录。 Now, to check the permissions, run: 现在,要检查权限,请运行:

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. 您自己的用户名将显示在username的位置,并且创建日期自然会有所不同,但否则输出将几乎相同。 The first part of the line is the revelant part. 该行的第一部分是启示性的部分。 The letters r and w specify read and write permissions. 字母rw指定读取和写入权限。

We're going to add execute permissions which will appear as an x in that line. 我们将添加执行权限,该权限将在该行中显示为x。 To add execute permissions, run the following command. 要添加执行权限,请运行以下命令。

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. x的存在指示文件可以直接运行而无需先调用Ruby。 The following command should get our file to say "hello." 以下命令应使我们的文件说“你好”。

./greeter.rb

Almost there. 差不多好了。 Now, we just need to get rid of the prefix ./ , which tells Bash where to look for greeter.rb , ie, in the current directory. 现在,我们只需要删除前缀./ ,该前缀告诉Bash在当前目录中寻找greeter.rb Before we complete this last step, though, let's rename our file to just greeter . 不过,在完成最后一步之前,让我们将文件重命名为greeter

mv greeter.rb greeter

Now, for the last step. 现在,最后一步。 Everytime we call a Bash program, eg, ls , chmod , mv , etc., Bash searches through a predefined list of folders looking for those programs. 每次我们调用Bash程序(例如lschmodmv等)时,Bash都会在预定义的文件夹列表中进行搜索以查找这些程序。 This is called the path. 这称为路径。 To see what the path is set to on your computer, try: 要查看计算机上设置的路径,请尝试:

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. 传统上,最好将/usr/bin//bin/类的文件夹保留下来。 Instead, any kind of user additions should be placed in /usr/local/bin/ . 相反,任何类型的用户添加都应放在/usr/local/bin/ If that folder doesn't exist, create it with: 如果该文件夹不存在,请使用以下命令创建它:

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. 现在,我们可以将问候语移动到该文件夹​​中,或者将应用程序保留在原位置,仅在/usr/local/bin/文件夹中创建一个软链接(或OS X术语中的别名)。 To create an alias, we'll use the ln command. 要创建别名,我们将使用ln命令。 From the directory where greeter lives, type: greeter所在的目录中,键入:

ln -s $PWD/greeter /usr/local/bin/ Note that the $PWD variable will expand to an absolute path to our greeter script. ln -s $PWD/greeter /usr/local/bin/注意, $PWD变量将扩展到我们的greeter脚本的绝对路径。 Now, we're done and we can simply type greeter to invoke our Ruby script! 现在,我们完成了,我们可以简单地键入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>查找其手册页。

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

相关问题 如何在我的bash脚本中运行ruby命令而不出现“红宝石:找不到命令” - How do I run ruby commands in my bash script without getting “ruby: command not found” 如何为这些bash和ruby命令编写bin /脚本? - How do I write a bin/script for these bash and ruby commands? 如何在Ruby脚本中为命令shell获取环境变量? - How do I source environment variables for a command shell in a Ruby script? 如何在Linux中作为Ruby脚本运行命令? - How do I to run a command in Linux as a Ruby script? 我们如何在ruby脚本之外持久化`source bash_profile`命令? - How do we persist a `source bash_profile` command outside of a ruby script? 如何让Ruby脚本每秒运行一次? - How do I make a Ruby script run once a second? 红宝石脚本中的Bash命令-错误“找不到命令” - Bash command in ruby script - Error “Command not found” 如何遍历通过终端传递给Ruby脚本的bash数组变量的元素? - How do I loop through elements of a bash array variable passed to a Ruby script via terminal? 如何使ruby的`system`调用知道我的`.bash_profile`别名? - How do I make ruby's `system` call aware of my `.bash_profile` aliases? 当从Ruby脚本作为命令%x [rspec]运行时,如何将RSpec输出到控制台? - How can I make RSpec output to console when run as a command %x[rspec] from Ruby script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM