当前位置: 首页 > 工具软件 > Ruby > 使用案例 >

使用命令行运行Ruby脚本

洪雨石
2023-12-01

Before really starting to use Ruby, you need to have a basic understanding of the command line. Since most Ruby scripts won't have graphical user interfaces, you'll be running them from the command line. Thus, you'll need to know, at the very least, how to navigate the directory structure and how to use pipe characters (such as |, < and >) to redirect input and output. The commands in this tutorial are the same on Windows, Linux, and OS X.

在真正开始使用Ruby之前,您需要对命令行有基本的了解。 由于大多数Ruby脚本都没有图形用户界面,因此您将从命令行运行它们。 因此,您至少需要知道如何导航目录结构以及如何使用管道字符(例如|<> )来重定向输入和输出。 本教程中的命令在Windows,Linux和OS X上相同。

  • To start a command prompt on Windows, go to Start -> Run. In the dialog that appears, enter cmd into the input box and press OK.

    要在Windows上启动命令提示符,请转到开始->运行 。 在出现的对话框中,在输入框中输入cmd ,然后按OK。

  • To start a command prompt on Ubuntu Linux, go to Applications -> Accessories -> Terminal.

    要在Ubuntu Linux上启动命令提示符,请转到应用程序->附件->终端

  • To start a command prompt on OS X, go to Applications -> Utilities -> Terminal.

    要在OS X上启动命令提示符,请转到应用程序->实用程序->终端

Once you're at the command line, you'll be presented with a prompt. It's often a single character such as $ or #. The prompt may also contain more information, such as your username or your current directory. To enter a command all you need to do is type in the command and hit the enter key.

进入命令行后,系统会提示您。 通常是单个字符,例如$ 。 该提示可能还包含更多信息,例如您的用户名或当前目录。 要输入命令,只需输入命令并按Enter键。

The first command to learn is the cd command, which will be used to get to the directory where you keep your Ruby files. The command below will change directory to the \scripts directory. Note that on Windows systems, the backslash character is used to delimit directories but on Linux and OS X, the forward slash character is used.

第一个要学习的命令是cd命令,它将用于进入保存Ruby文件的目录。 下面的命令会将目录更改为\ scripts目录。 请注意,在Windows系统上,反斜杠字符用于分隔目录,而在Linux和OS X上,反斜杠字符用于分隔目录。

Running Ruby Scripts

运行Ruby脚本

Now that you know how to navigate to your Ruby scripts (or your rb files), it's time to run them. Open your text editor and save the following program as test.rb.

现在,您知道如何导航到Ruby脚本(或rb文件),是时候运行它们了。 打开您的文本编辑器,然后将以下程序另存为test.rb。

#!/usr/bin/env ruby
#!/ usr / bin / envRuby
 
print "What is your name? "
打印“你叫什么名字?”
name = gets.chomp
名称= gets.chomp
puts "Hello #{name}!"
放置“你好#{name}!”

Open a command line window and navigate to your Ruby scripts directory using the cd command. Once there, you can list files, using the dir command on Windows or the ls command on Linux or OS X. Your Ruby files will all have the .rb file extension. To run the test.rb Ruby script, run the command ruby test.rb. The script should ask you for your name and greet you.

打开命令行窗口,然后使用cd命令导航到Ruby脚本目录。 到那里后,您可以使用Windows上的dir命令或Linux或OS X上的ls命令列出文件。您的Ruby文件都将带有.rb文件扩展名。 要运行test.rb Ruby脚本,请运行命令ruby test.rb。 该脚本应询问您的名字并打招呼。

Alternatively, you can configure your script to run without using the Ruby command. On Windows, the one-click installer already set up a file association with the .rb file extension. Simply running the command test.rb will run the script. In Linux and OS X, for scripts to run automatically, two things must be in place: a "shebang" line and the file being marked as executable.

另外,您可以配置脚本以使其运行而无需使用Ruby命令。 在Windows上,一键安装程序已经设置了具有.rb文件扩展名的文件关联。 只需运行命令test.rb即可运行脚本。 在Linux和OS X中,要使脚本自动运行,必须有两件事:“ shebang”行和将文件标记为可执行文件。

The shebang line is already done for you; it's the first line in the script starting with #!. This tells the shell what type of file this is. In this case, it's a Ruby file to be executed with the Ruby interpreter. To mark the file as executable, run the command chmod +x test.rb. This will set a file permission bit indicating that the file is a program and that it can be run. Now, to run the program, simply enter the command ./test.rb.

shebang线路已经为您完成; 它是脚本中以开头的第一行 。 这告诉外壳这是什么文件类型。 在这种情况下,这是一个要用Ruby解释器执行的Ruby文件。 要将文件标记为可执行文件,请运行命令chmod + x test.rb。 这将设置一个文件许可权位,指示该文件是程序并且可以运行。 现在,要运行该程序,只需输入命令./test.rb

Whether you invoke the Ruby interpreter manually with the Ruby command or run the Ruby script directly is up to you. Functionally, they are the same thing. Use whichever method you feel most comfortable with.

您是使用Ruby命令手动调用Ruby解释器还是直接运行Ruby脚本由您决定。 从功能上讲,它们是同一件事。 使用您最喜欢的任何一种方法。

使用管道字符 ( Using Pipe Characters )

Using the pipe characters is an important skill to master, as these characters will alter the input or output of a Ruby script. In this example, the > character is used to redirect the output of test.rb to a text file called test.txt instead of printing to the screen.

使用管道字符是掌握的一项重要技能,因为这些字符会改变Ruby脚本的输入或输出。 在此示例中, >字符用于将test.rb的输出重定向到名为test.txt的文本文件,而不是打印到屏幕上。

If you open new test.txt file after you run the script, you'll see the output of the test.rb Ruby script. Knowing how to save output to a .txt file can be very useful. It allows you to save program output for careful examination or to be used as input to another script at a later time.

如果在运行脚本后打开新的test.txt文件,则会看到test.rb Ruby脚本的输出。 知道如何将输出保存到.txt文件可能非常有用。 它允许您保存程序输出以进行仔细检查,或在以后用作其他脚本的输入。

C:\scripts>ruby example.rb >test.txt
C:\ scripts> ruby​​ example.rb> test.txt

Similarly, by using the < character instead of the > character you can redirect any input a Ruby script may read from the keyboard to read from a .txt file. It's helpful to think of these two characters as funnels; you're funneling output to files and input from files.

同样,通过使用<字符而不是>字符,您可以重定向Ruby脚本可以从键盘读取的任何输入,以从.txt文件读取。 将这两个字符视为漏斗会有所帮助; 您正在将输出漏斗到文件,并将输入漏斗。

C:\scripts>ruby example.rb
C:\ scripts> ruby​​ example.rb

Then there's the pipe character, |. This character will funnel the output from one script to the input of another script. It's the equivalent of funneling the output of a script to a file, then funneling the input of a second script from that file. It just shortens the process.

然后是竖线字符| 。 该字符会将输出从一个脚本泄漏到另一个脚本的输入。 这等效于将脚本的输出漏斗到文件,然后从该文件漏斗第二个脚本的输入。 它只是缩短了过程。

The | character is useful in creating "filter" type programs, where one script generates unformatted output and another script formats the output to the desired format. Then the second script could be changed or replaced entirely without having to modify the first script at all.

| 字符对于创建“过滤器”类型的程序很有用,其中一个脚本生成未格式化的输出,而另一个脚本将输出格式化为所需的格式。 然后,可以完全更改或替换第二个脚本,而不必完全修改第一个脚本。

C:\scripts>ruby example1.rb | ruby example2.rb
C:\ scripts> ruby​​ example1.rb | Rubyexample2.rb

交互式Ruby提示 ( The Interactive Ruby Prompt )

One of the great things about Ruby is that it's test-driven. The interactive Ruby prompt provides an interface to the Ruby language for instant experimentation. This comes in handy while learning Ruby and experimenting with things like regular expressions. Ruby statements can be run and the output and return values can be examined immediately. If you make a mistake, you can go back and edit your previous Ruby statements to correct those mistakes.

Ruby的一大优点是它是测试驱动的。 交互式Ruby提示提供了Ruby语言的界面,可以立即进行实验。 在学习Ruby并尝试使用正则表达式之类的东西时,这非常方便。 可以运行Ruby语句,并且可以立即检查输出和返回值。 如果输入有误,则可以返回并编辑以前的Ruby语句以更正这些错误。

To start the IRB prompt, open your command-line and run the irb command. You'll be presented with the following prompt:

要启动IRB提示符,请打开命令行并运行irb命令。 系统将显示以下提示:

irb(main):001:0>
irb(main):001:0>

Type the "hello world" statement we've been using into the prompt and hit Enter. You'll see any output the statement generated as well as the return value of the statement before being returned to the prompt. In this case, the statement output "Hello world!" and it returned nil.

在提示中输入我们一直在使用的“ hello world”语句,然后按Enter。 在返回到提示之前,您将看到该语句生成的所有输出以及该语句的返回值。 在这种情况下,语句输出“ Hello world!”。 然后返回nil

irb(main):001:0> puts "Hello world!"
irb(main):001:0>放置“ Hello world!”
Hello world!
你好,世界!
=> nilf
=>尼尔夫
irb(main):002:0>
irb(主要):002:0>

To run this command again, simply press the up key on your keyboard to get to the statement you previously ran and press the Enter key. If you want to edit the statement before running it again, press the left and right arrow keys to move the cursor to the correct place in the statement. Make your edits and press Enter to run the new command. Pressing up or down additional times will allow you to examine more of statements you've run.

要再次运行此命令,只需按键盘上的向上键进入先前运行的语句,然后按Enter键。 如果要在再次运行该语句之前对其进行编辑,请按左右箭头键将光标移至该语句中的正确位置。 进行编辑,然后按Enter以运行新命令 。 额外增加或减少时间可以让您检查更多已运行的语句。

The interactive Ruby tool should be used throughout learning Ruby. When you learn about a new feature or just want to try something, start up the interactive Ruby prompt and try it. See what the statement returns, pass different parameters to it and just do some general experimenting. Trying something yourself and seeing what it does can be a lot more valuable than just reading about it!

在学习Ruby的整个过程中都应使用交互式Ruby工具。 当您了解新功能或只想尝试某些功能时,请启动交互式Ruby提示并进行尝试。 查看该语句返回的内容,将不同的参数传递给它,然后进行一些常规实验。 自己尝试一下,看看它能做什么,比仅仅阅读它有价值得多!

翻译自: https://www.thoughtco.com/using-the-command-line-2908368

 类似资料: