如何从终端中的任何路径运行.sh脚本? [英] How to run a .sh-script from any path in a terminal?
问题描述
我知道如何运行我创建的脚本.但是,我需要通过终端更改目录并运行我的脚本,这是一个痛苦的问题.
我需要运行已进入Desktop的slowloris
脚本,现在将目录更改为Desktop并运行.
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.
然后我有另一个根.现在将目录更改为root并运行.
Then I have another in root; now change the directory to root and run that.
我的问题是:
如何通过从任意路径键入./script
来运行任何shell脚本,就像我们通过从任意路径给出msfconsole
从任何路径启动Metasploit一样.
My question is:
How can I run any shell-script by just typing ./script
from any path like we start Metasploit from any path by giving msfconsole
from any path.
推荐答案
一种选择就是简单地键入脚本的路径:
One option is simply to type the path to the script:
~/Desktop/script
这很好,但是有点笨拙.
This works fine, but gets a bit unwieldy.
这是PATH
环境变量的作用.这就是$HOME/bin
的目的.
This is what the PATH
environment variable is for. And it is what $HOME/bin
is for.
- 创建一个目录
$HOME/bin
.将所有可执行脚本放入其中(如果需要,请使用chmod +x script
使它们可执行 †† ).这样,就可以在一个地方查找要运行的脚本. - 将
$HOME/bin
添加到您的PATH
.我把我的放在前面:PATH="$HOME/bin:$PATH
,但是如果愿意,可以把它放在后面. - 更新您的
.profile
或.bash_profile
(或可能是.bashrc
)文件以设置PATH
.不过,要提防不断增长的PATH.
- 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.
为三合一
As tripleee noted, 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.
我的$HOME/bin
目录中大约有500个脚本和程序.
I have about 500 scripts and programs in my $HOME/bin
directory.
请注意,这不需要任何特殊特权.如果您具有计算机的管理员访问权限,并且认为其他用户可能会觉得您的命令有用,那么可以将脚本/程序安装在PATH
上系统提供的目录之一中.但是,通常最好不要将程序添加到以下任何一个程序中:
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:
-
/bin
-
/usr/bin
-
/sbin
-
/usr/sbin
/bin
/usr/bin
/sbin
/usr/sbin
通常是/usr/local/bin
,这是系统未提供的广泛使用的命令的合适位置.
There is often/usually /usr/local/bin
which is a suitable place for widely used commands not provided by the system.
†† 最好使用chmod a+x,go-w script
;您的脚本不应为其他人所写.您甚至可以简单地使用chmod 555 script
或chmod 755 script
.我倾向于保持脚本不可写.这样,我必须对版本控制系统进行正式的更改过程.这意味着不受控制的变更的可能性较小.
†† 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.
这篇关于如何从终端中的任何路径运行.sh脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!