bash 如何从终端中的任何路径运行 .sh-script?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29235154/
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
How to run a .sh-script from any path in a terminal?
提问by Mastan
I know how to run the script I created. But it is a matter of pain that I need to change directory through terminal and run my scripts.
I need to run the slowloris
script, that has into Desktop, now change directory to Desktop and run.
我知道如何运行我创建的脚本。但是我需要通过终端更改目录并运行我的脚本是一件痛苦的事情。我需要运行slowloris
已进入桌面的脚本,现在将目录更改为桌面并运行。
Then I have another in root; now change the directory to root and run that.
然后我在 root 中有另一个;现在将目录更改为 root 并运行它。
My question is:
How can I run any shell-script by just typing ./script
from any pathlike we start Metasploit from any path by giving msfconsole
from any path.
我的问题是:
如何通过./script
从任何路径输入来运行任何 shell 脚本,就像我们通过msfconsole
从任何路径给出从任何路径启动 Metasploit 一样。
采纳答案by Jonathan Leffler
One option is simply to type the path to the script:
一种选择是简单地键入脚本的路径:
~/Desktop/script
This works fine, but gets a bit unwieldy.
这工作正常,但有点笨拙。
This is what the PATH
environment variable is for. And it is what $HOME/bin
is for.
这就是PATH
环境变量的用途。这就是为什么$HOME/bin
。
- Create yourself a directory
$HOME/bin
. Put all your executable scripts in it (make them executable withchmod +x script
if need be??). This way, there's one place to look for the scripts you want to run. - Add
$HOME/bin
to yourPATH
. I put mine at the front:PATH="$HOME/bin:$PATH
, but you could put it at the back if you prefer. - Update your
.profile
or.bash_profile
(or possibly.bashrc
) file to setPATH
. Beware of a continually growing PATH, though.
- 为自己创建一个目录
$HOME/bin
。将所有可执行脚本放入其中(chmod +x script
如果需要,使它们可执行??)。这样,您就可以在一个地方查找要运行的脚本。 - 添加
$HOME/bin
到您的PATH
. 我把我的放在前面:PATH="$HOME/bin:$PATH
,但如果你愿意,你可以把它放在后面。 - 更新您的
.profile
或.bash_profile
(或可能的.bashrc
)文件以设置PATH
. 但是,请注意不断增长的 PATH。
As tripleeenoted, once the command is installed in a directory on PATH
, you no longer type ./script
, but just script
. This is exactly like you type ls
and not /bin/ls
, etc. Once the program is installed in a directory on your PATH
, it is (for many purposes) indistinguishable from a system-provided command.
正如tripleee 所指出的,一旦命令安装在一个目录中PATH
,您就不再键入./script
,而只是键入script
。这与您键入ls
而不是/bin/ls
等完全一样。一旦该程序安装在您的 目录中PATH
,它(出于许多目的)就与系统提供的命令无法区分。
I have about 500 scripts and programs in my $HOME/bin
directory.
我的目录中有大约 500 个脚本和程序$HOME/bin
。
Note that this doesn't require any special privileges. If you have administrator access to your machine and you think other users might find your commands useful, then you could install the scripts/programs in one of the system-provided directories on your PATH
. However, it is usually best not to add programs to any of:
请注意,这不需要任何特殊权限。如果您对您的机器具有管理员访问权限,并且您认为其他用户可能会发现您的命令有用,那么您可以将脚本/程序安装在PATH
. 但是,通常最好不要将程序添加到以下任何一项:
/bin
/usr/bin
/sbin
/usr/sbin
/bin
/usr/bin
/sbin
/usr/sbin
There is often/usually /usr/local/bin
which is a suitable place for widely used commands not provided by the system.
通常/通常/usr/local/bin
这是系统未提供的广泛使用的命令的合适位置。
??It would be better to use chmod a+x,go-w script
; your scripts should not be writable by other people. You could even simply use chmod 555 script
or chmod 755 script
. I tend to keep my scripts non-writable. That way, I have to go through a formal change process with the version control system. It means there's less danger of uncontrolled changes.
?? 最好使用chmod a+x,go-w script
; 您的脚本不应被其他人写入。你甚至可以简单地使用chmod 555 script
orchmod 755 script
。我倾向于保持我的脚本不可写。这样,我必须通过版本控制系统进行正式的更改过程。这意味着不受控制的变化的风险较小。
回答by Tom Cammann
You have to copy or link the script into a directory that is on the $PATH. Usually /usr/bin
and /usr/local/bin/
are on the path so these are good locations to link or copy the script to.
您必须将脚本复制或链接到 $PATH 上的目录中。通常/usr/bin
并且/usr/local/bin/
在路径上,因此这些是链接或复制脚本的好位置。
ln -s /path/to/your/script /usr/local/bin
If you are not root you will either need to sudo
that command or run it as the root user.
如果您不是 root,您将需要sudo
该命令或以 root 用户身份运行它。