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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:37:08  来源:igfitidea点击:

How to run a .sh-script from any path in a terminal?

linuxbashshellunixterminal

提问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 slowlorisscript, 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 ./scriptfrom any pathlike we start Metasploit from any path by giving msfconsolefrom 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 PATHenvironment variable is for. And it is what $HOME/binis for.

这就是PATH环境变量的用途。这就是为什么$HOME/bin

  1. Create yourself a directory $HOME/bin. Put all your executable scripts in it (make them executable with chmod +x scriptif need be??). This way, there's one place to look for the scripts you want to run.
  2. Add $HOME/binto your PATH. I put mine at the front: PATH="$HOME/bin:$PATH, but you could put it at the back if you prefer.
  3. Update your .profileor .bash_profile(or possibly .bashrc) file to set PATH. Beware of a continually growing PATH, though.
  1. 为自己创建一个目录$HOME/bin。将所有可执行脚本放入其中(chmod +x script如果需要,使它们可执行??)。这样,您就可以在一个地方查找要运行的脚本。
  2. 添加$HOME/bin到您的PATH. 我把我的放在前面: PATH="$HOME/bin:$PATH,但如果你愿意,你可以把它放在后面。
  3. 更新您的.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 lsand 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/bindirectory.

我的目录中有大约 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/binwhich 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 scriptor 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 scriptorchmod 755 script。我倾向于保持我的脚本不可写。这样,我必须通过版本控制系统进行正式的更改过程。这意味着不受控制的变化的风险较小。

回答by Tom Cammann

You have to copy or link the script into a directory that is on the $PATH. Usually /usr/binand /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 sudothat command or run it as the root user.

如果您不是 root,您将需要sudo该命令或以 root 用户身份运行它。