简体   繁体   English

在linux中运行sh文件时,为什么必须运行./name.sh?

[英]When running a sh file in linux, why do I have to run ./name.sh?

I have a file called x.sh that I want to execute. 我有一个名为x.sh的文件,我想执行它。 If I run: 如果我跑:

x.sh

then I get: 然后我得到:

x.sh: command not found

If I run: 如果我跑:

./x.sh

then it runs correctly. 然后它运行正常。 Why do I have to type in ./ first? 为什么我必须首先输入./?

Because the current directory is normally not included in the default PATH, for security reasons: by NOT looking in the current directory all kinds of nastiness that could be caused by planting a malicious program with the name of a legitimate utility can be avoided. 因为当前目录通常不包含在默认PATH中,出于安全原因:通过不查看当前目录,可以避免因种植具有合法实用程序名称的恶意程序而导致的各种恶意。 As an example, imagine someone manages to plant a script called ls in your directory, and that script executes rm * . 例如,假设有人设法在您的目录中ls一个名为ls的脚本,该脚本执行rm *

If you wish to include the current directory in your path, and you're using bash as your default shell, you can add the path via your ~/.bashrc file. 如果您希望在路径中包含当前目录,并且您使用bash作为默认shell,则可以通过~/.bashrc文件添加路径。

export PATH=$PATH:.

Based on the explanation above, the risk posed by rogue programs is reduced by looking in . 基于上述解释,通过查看减少了流氓程序带来的风险. last, so all well known legitimate programs will be found before . 最后,所以众所周知的合法程序将在之前找到。 is checked. 检查。

You could also modify the systemwide settings via /etc/profile but that's probably not a good idea. 可以通过/etc/profile修改系统范围的设置,但这可能不是一个好主意。

Because the current directory is not into the PATH environment variable by default, and executables without a path qualification are searched only inside the directory specified by PATH . 因为默认情况下当前目录不在PATH环境变量中,所以只在PATH指定的目录内搜索没有路径限定的可执行文件。 You can change this behavior by adding . 您可以通过添加更改此行为. to the end of PATH , but it's not common practice, you'll just get used to this UNIXism. PATH结束,但这不常见,你只会习惯这种UNIX主义。

The idea behind this is that, if executables were searched first inside the current directory, a malicious user could put inside his home directory an executable named eg ls or grep or some other commonly used command, tricking the administrator to use it, maybe with superuser powers. 这背后的想法是,如果首先在当前目录中搜索可执行文件,恶意用户可以在其主目录中放入名为例如lsgrep或其他常用命令的可执行文件,欺骗管理员使用它,可能与超级用户权力。 On the other hand, this problem is not much felt if you put . 另一方面,如果你把这个问题感觉不大. at the end of PATH , since in that case the system directories are searched first. PATH的末尾,因为在那种情况下首先搜索系统目录。

But : our malicious user could still create his dangerous scripts named as common typos of often used commands, eg sl for ls (protip: bind it to Steam Locomotive and you won't be tricked anyway :D ). 但是 :我们的恶意用户仍然可以创建他的危险脚本,命名为常用命令的常见错别字,例如sl for ls (protip:将它绑定到Steam机车 ,你不会被欺骗:D )。

So you see that it's still better to be safe that, if you type an executable name without a path qualification, you are sure you're running something from system directories (and thus supposedly safe). 所以你会发现,如果你输入一个没有路径限定条件的可执行文件名称,那么保持安全仍然会更好,你确定你正在从系统目录中运行一些东西(因此可以说是安全的)。

Because current directory is not in PATH (unlike cmd in Windows). 因为当前目录不在PATH中(与Windows中的cmd不同)。 It is a security feature so that malicious scripts in your current directory are not accidentally run. 它是一种安全功能,因此不会意外运行当前目录中的恶意脚本。

Though it is not advisable, to satisfy curiosity, you can add . 虽然不建议,但为了满足好奇心,你可以添加. to the PATH and then you will see that x.sh will work. 到PATH然后你会看到x.sh会起作用。

If you don't explicitly specify a directory then the shell searches through the directories listed in your $PATH for the named executable. 如果您没有显式指定目录,那么shell将搜索$PATH列出的目录以查找指定的可执行文件。 If your $PATH does not include . 如果您的$PATH不包括. then the current directory is not searched. 然后不搜索当前目录。

$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

This is on purpose. 这是故意的。 If the current directory were searched then the command you type could potentially change based on what directory you're in. This would allow a malicious user to place a binary named ls or cp into directories you frequent and trick you into running a different program. 如果搜索了当前目录,那么您键入的命令可能会根据您所在的目录而发生更改。这将允许恶意用户将名为lscp的二进制文件放入您经常访问的目录中,并诱使您运行其他程序。

$ cat /tmp/ls
rm -rf ~/*
$ cd /tmp
$ ls
*kaboom*

I strongly recommend you not add . 我强烈建议你不要添加. to your $PATH . 你的$PATH You will quickly get used to typing ./ , it's no big deal. 你会很快习惯打字./ ,这没什么大不了的。

You can't execute your file by typing simply x.sh because the present working directory isn't in your $PATH. 您只能通过输入x.sh来执行文件,因为当前的工作目录不在$ PATH中。 To see your present working directory, type 要查看当前的工作目录,请键入

$ pwd

To see your $PATH, type 要查看$ PATH,请键入

$ echo $PATH

To add the current directory to your $PATH for this session, type 要将当前目录添加到此会话的$ PATH,请键入

$ PATH=$PATH:.

To add it permanently, edit the file .profile in your home directory. 要永久添加它,请在主目录中编辑.profile文件。

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

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