我试图在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
发布评论
评论(2)
?
可以以与编译程序相同的方式执行Shell脚本。该过程使用
exec*
family的函数 ./ foo 作为执行程序。这些功能基于系统调用。然后,OS/内核检测文件是可以直接执行的编译程序还是必须传递给解释器的脚本文件。如果文件包含Shebang行,则操作系统将执行指定的解释器,该解释器不必是外壳,否则它将运行默认的外壳。脚本文件作为参数传递给解释器。
Shell正在使用普通用户权限运行,并在尝试打开脚本文件时会出现错误。
您可以在函数家族或在 linorefrer“对于
执行
。搜索单词解释器。您可以检查Linux内核源代码以获取更多详细信息。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 forexecve
. Search for the word interpreter. You could check the Linux kernel source code for more details.您会遇到此错误,因为脚本本身没有执行或阅读许可。要在Shell中运行脚本(在您的途中),您需要已阅读和执行权限:
运行脚本的另一种方法是:
在这种情况下,您不需要执行权限。对于记录,标准方法是使用构造:
您无需逃脱感叹号
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:
the other way to run the script is:
and you do not need execution permission in this case. For the record the standard way is to use construction like:
You do not need to escape exclamation mark