设置路径变量并运行 Ruby 脚本 [英] Setting path variables and running Ruby script

查看:28
本文介绍了设置路径变量并运行 Ruby 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用 Ruby 脚本,为了运行这个脚本,我必须先 cd 进入项目的根目录,即 /usr/local/bin/youtube-multiple-dl 然后以 bin/youtube-multiple-dl 的形式执行脚本.

This is my first time working with a Ruby script, and, in order to run this script, I have to first cd into the root of the project, which is /usr/local/bin/youtube-multiple-dl and then execute the script as bin/youtube-multiple-dl.

我尝试设置 PATH 变量

I tried setting the PATH variable

echo 'export PATH="$HOME/youtube-multiple-dl/bin:$PATH"' >> ~/.bash_profile

希望我可以从机器上的任何地方运行它,而不必cd到项目的根目录,但是,到目前为止还没有运气.

in hopes that I can run this from anywhere on the machine without having to cd to the project's root, however, no luck with that so far.

系统:Ubuntu 15.04 服务器

System: Ubuntu 15.04 server

脚本仓库

我目前执行脚本的方式是:

My current way of executing the script is:

root@box15990:~# cd /usr/local/bin/youtube-multiple-dl
root@box15990:/usr/local/bin/youtube-multiple-dl# bin/youtube-multiple-dl

期望的脚本执行方式:

root@box15990:~# youtube-multiple-dl

如何正确设置此脚本的环境路径以便从任何地方运行?

How can I properly set the enviroment path for this script in order to run from anywhere?

推荐答案

echo 'export PATH="$HOME/youtube-multiple-dl/bin:$PATH"' >> ~/.bash_profile

这不是我们设置 PATH 条目的方式.

isn't how we set a PATH entry.

PATH 是要搜索的目录列表,而不是文件列表.

The PATH is a list of directories to be searched, not a list of files.

通常,PATH 应包含以下内容:

Typically, the PATH should contain something like:

/usr/local/bin:/usr/bin

在其中的某个地方.

如果没有,那么您想使用文本编辑器修改它,例如 nanopicovim 使用 其中一个:

If it doesn't, then you want to modify it using a text editor, such as nano, pico or vim using one of these commands:

nano ~/.bash_profile
pico ~/.bash_profile
vim ~/.bash_profile

你可能想要前两个中的一个而不是 vim 作为 vim,虽然它非常强大并且是世界上最常用的编辑器之一,但如果你不使用它也不会过于直观到它.您也可以使用 man nanoman pico 来了解另一个.

You probably want one of the first two over vim as vim, while being extremely powerful and one of the most-used editors in the world, is also not overly intuitive if you're not used to it. You can use man nano or man pico to learn about the other too.

进入文件编辑器后,滚动到底部并删除您添加的行.然后在你的 PATH 中找到 /usr/bin 部分并在它之前添加 /usr/local/bin: .: 是目录之间的分隔符.该更改将告诉 shell 在 /usr/bin 之前查看 /usr/local/bin,以便您添加到 /usr/local/bin 目录位于系统安装代码之前,位于/usr/bin.

Once your in your file editor, scroll to the bottom and remove the line you added. Then find the /usr/bin section in your PATH and add /usr/local/bin: before it. : is the delimiter between directories. That change will tell the shell to look in /usr/local/bin before /usr/bin, so that any things you added to the /usr/local/bin directory will be found before the system-installed code, which is in /usr/bin.

文件中可能没有 PATH 语句.如果您没有看到,只需添加:

It's possible that there isn't a PATH statement in the file. If you don't see one, simply add:

export PATH=/usr/local/bin:$PATH

修改你的 ~/.bash_profile 后,保存文件并退出编辑器,然后重新启动你的 shell.您可以通过退出并重新打开终端窗口或运行:

After modifying your ~/.bash_profile, save the file and exit the editor, and then restart your shell. You can do that by exiting and re-opening a terminal window, or by running:

 exec $SHELL

在命令行.

此时,运行:

echo $PATH

应该反映你的路径的变化.

should reflect the change to your path.

要确认更改生效,您可以运行:

To confirm that the change is in effect, you can run:

which youtube-multiple.dl

你应该回来:

/usr/local/bin/youtube-multiple.dl

此时你应该能够运行:

youtube-multiple.dl -h

并返回显示内置帮助的响应.这是因为 shell 将搜索路径,从第一个定义的目录开始,一直继续直到用尽列表,然后执行第一个匹配该名称的文件.

and get back a response showing the built-in help. This is because the shell will search the path, starting with the first defined directory, and continue until it exhausts the list, and will execute the first file matching that name.

由于您遇到的困难,我强烈建议您阅读一些有关管理 *nix 系统的教程.学习基础知识并不太难,了解 shell 如何查找文件并执行它们对于编写脚本语言(如 Ruby、Python、Perl 等)的任何人来说都是必不可少的.我们一直在使用操作系统,为系统和用户的使用,正确、安全地操作对机器的安全性和稳定性非常重要.

Because of the difficulties you're having, I'd strongly recommend reading some tutorials about managing a *nix system. It's not overly hard to learn the basics, and having an understanding of how the shell finds files and executes them is essential for anyone programming a scripting language like Ruby, Python, Perl, etc. We're using the OS constantly, installing files for system and user's use, and doing so correctly and safely is very important for the security and stability of the machine.

这篇关于设置路径变量并运行 Ruby 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆