#!舍邦工作? [英] How does the #! shebang work?

查看:12
本文介绍了#!舍邦工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在脚本中,您必须在第一行包含一个 #!,然后是将执行脚本的程序的路径(例如:sh、perl).

In a script you must include a #! on the first line followed by the path to the program that will execute the script (e.g.: sh, perl).

据我所知,# 字符表示注释的开始,并且该行应该被执行脚本的程序忽略.看起来,第一行在某个时候被某些东西读取,以便脚本由正确的程序执行.

As far as I know, the # character denotes the start of a comment and that line is supposed to be ignored by the program executing the script. It would seem, that this first line is at some point read by something in order for the script to be executed by the proper program.

有人可以更详细地了解 #! 的工作原理吗?

Could somebody please shed more light on the workings of the #!?

我真的对此很好奇,所以答案越深入越好.

I'm really curious about this, so the more in-depth the answer the better.

推荐答案

推荐阅读:

unix 内核的程序加载器负责执行此操作.当 exec() 被调用时,它会要求内核从文件中的参数加载程序.然后它将检查文件的前 16 位以查看它具有什么可执行格式.如果它发现这些位是 #! 它将使用文件第一行的其余部分来查找它应该启动的程序,并提供它试图启动的文件的名称(脚本)作为解释器程序的最后一个参数.

The unix kernel's program loader is responsible for doing this. When exec() is called, it asks the kernel to load the program from the file at its argument. It will then check the first 16 bits of the file to see what executable format it has. If it finds that these bits are #! it will use the rest of the first line of the file to find which program it should launch, and it provides the name of the file it was trying to launch (the script) as the last argument to the interpreter program.

解释器然后正常运行,并将 #! 视为注释行.

The interpreter then runs as normal, and treats the #! as a comment line.

这篇关于#!舍邦工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆