为什么外壳知道未经阅读许可的文件中指定的解释器

发布于 2025-01-31 06:36:56 字数 688 浏览 4 评论 0 原文

我试图在Linux上学习外壳, 但是我有一个问题似乎令人困惑。

  • 我的环境是:
    • OS:Manjaro 21.2.6 Qonos
    • 内核:x86_64 linux 5.15.38-1-manjaro
    • shell:zsh 5.8.1(x86_64-pc-linux-gnu)
  • 问题是:
    • 我创建了一个名为 foo 的文件,然后回音#\!/bin/sh/sh ,并获得了文件 foo 的许可通过使用 chmod
    • 文件 foo 确实没有读或写入许可,这是对的,
    • 但是,当我执行命令 ./ foo 时,我得到了错误/bin/sh:./ foo:许可拒绝

那么,为什么外壳知道文件中的shebang foo 没有读取许可?

如果你们中有人可以主张任何建议,我将非常感谢!

行为-Example

I am trying to learn shell on Linux,
but I've got a problem which seems confusing.

  • My environment is:
    • OS: Manjaro 21.2.6 Qonos
    • Kernel: x86_64 Linux 5.15.38-1-MANJARO
    • Shell: zsh 5.8.1 (x86_64-pc-linux-gnu)
  • The problem is:
    • I created a file named foo, and echoed #\!/bin/sh to it, and the permission of file foo has been modified to 100 by using chmod.
    • The file foo doesn't have the read or write permission indeed, that's for true,
    • but when I executed the command ./foo, I got the error /bin/sh: ./foo: permission denied.

So why the Shell knows what the shebang in the file foo is without the read permission ???

If anyone of you can proide any suggesstions, I will be really thankful !

behavior-example

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风流物 2025-02-07 06:36:56

那么,为什么外壳知道文件中的shebang没有读取许可?

可以以与编译程序相同的方式执行Shell脚本。该过程使用 exec* family的函数 ./ foo 作为执行程序。这些功能基于系统调用。

然后,OS/内核检测文件是可以直接执行的编译程序还是必须传递给解释器的脚本文件。如果文件包含Shebang行,则操作系统将执行指定的解释器,该解释器不必是外壳,否则它将运行默认的外壳。脚本文件作为参数传递给解释器。

Shell正在使用普通用户权限运行,并在尝试打开脚本文件时会出现错误。


您可以在函数家族或在 linorefrer“对于执行 。搜索单词解释器。您可以检查Linux内核源代码以获取更多详细信息。

So why the Shell knows what the shebang in the file foo is without the read permission ???

It is not the shell that reads the shebang line but the OS/kernel.

A shell script can be executed in the same way as a compiled program. The process uses a function of the exec* family and passes ./foo as the program to execute. These functions are based on system calls.

The OS/kernel then detects if the file is a compiled program which can be executed directly or a script file which must be passed to an interpreter. If the file contains a shebang line, the OS will execute the specified interpreter, which does not have to be a shell, otherwise it will run the default shell. The script file is passed as an argument to the interpreter.

The shell is running with normal user permissions and will get an error when it tries to open the script file.


You can find some information about executiong scripts in the POSIX specification of the exec function family or in the Linux manual page for execve. Search for the word interpreter. You could check the Linux kernel source code for more details.

忘东忘西忘不掉你 2025-02-07 06:36:56

您会遇到此错误,因为脚本本身没有执行或阅读许可。要在Shell中运行脚本(在您的途中),您需要已阅读和执行权限:

chmod 500 foo
./foo

运行脚本的另一种方法是:

sh foo

在这种情况下,您不需要执行权限。对于记录,标准方法是使用构造:

#!/bin/sh

您无需逃脱感叹号

You get this error because the script itself do not have execution or read permission. To run script in shell (on your way) you need to have read and execute permissions:

chmod 500 foo
./foo

the other way to run the script is:

sh foo

and you do not need execution permission in this case. For the record the standard way is to use construction like:

#!/bin/sh

You do not need to escape exclamation mark

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文