Linux Ruby:从 bash 脚本运行脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5236471/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:10:07  来源:igfitidea点击:

Ruby: Run script from bash script?

rubylinuxbashscripting

提问by eckza

I'm trying to call a Ruby script from a bash script, but no luck.

我正在尝试从 bash 脚本调用 Ruby 脚本,但没有运气。

#!/bin/bash
ruby -v

works just fine, so I know that it's not Ruby, but

工作得很好,所以我知道它不是 Ruby,但是

#!/bin/bash
ruby bash_test.rb

does not. Here's the fun part:

才不是。这是有趣的部分:

john@starfire:~/Desktop$ bash ubuntu_cmds.sh
(LoadError)h file or directory -- bash_test.rb
john@starfire:~/Desktop$ ls *.rb
bash_test.rb

Both files are sitting on my desktop.

这两个文件都在我的桌面上。

ruby bash_test.rb

works just fine, too.

也很好用。

I'm new to bash scripting, so I'm pretty sure that I'm just making a stupid error.

我是 bash 脚本的新手,所以我很确定我只是犯了一个愚蠢的错误。

I'm running Ubuntu 10.10 with Ruby 1.8.7. Thanks in advance for any help or advice.

我正在使用 Ruby 1.8.7 运行 Ubuntu 10.10。在此先感谢您的任何帮助或建议。

EDIT: Deleted the .sh and the .rb and started over, and made sure to chmod +xthe .sh, and it worked on the first try. I have no idea why. Thanks for the help, though.

编辑:删除 .sh 和 .rb 并重新开始,并确保chmod +x.sh,并且它在第一次尝试时起作用。我不知道为什么。不过还是谢谢你的帮助。

采纳答案by Chaim Geretz

Do you have a carriage return in the bash script after the file name ?

bash 脚本中的文件名后是否有回车符?



EDITED

已编辑

Double check how the filename is being passed to ruby in the bash script.

仔细检查文件名是如何在 bash 脚本中传递给 ruby​​ 的。

Error output should be as below if the file wasn't found

如果找不到文件,错误输出应如下所示

ruby: No such file or directory -- bash_test.rb (LoadError)

From what you are displaying as an error it appears that there is a carriage return that is being assumed by ruby as part of the filename so you are getting the following error output.

从您显示为错误的内容来看,ruby 将回车视为文件名的一部分,因此您将获得以下错误输出。

 (LoadError)h file or directory -- bash_test.rb

回答by rubyprince

try

尝试

#!/bin/bash
ruby ~/Desktop/bash_test.rb

回答by Andrew Grimm

You may have to do ruby ./bash_test.rb, as sometimes .isn't in your $PATH.

您可能必须这样做ruby ./bash_test.rb,因为有时.不在您的$PATH.