简体   繁体   English

Shell命令如何执行

[英]How shell commands execute

I am a newbee and looking for some info. 我是新手,正在寻找一些信息。

Thanks in advance. 提前致谢。

  1. What is difference between echo "Hello World!" echo“ Hello World!”和有什么不一样? and a c-program which prints "Hello World!" 和一个打印“ Hello World!”的C程序 using printf. 使用printf。

  2. How do shell commands get executed. Shell命令如何执行。 For example if I give ls it lists all the files in the directory. 例如,如果我给ls它列出目录中的所有文件。 Is there executable binary which is run when we enter ls in shell. 当我们在shell中输入ls时,是否有运行的可执行二进制文件。

Please let me know if you guys have any links or source to get this clear. 请让我知道你们是否有任何链接或消息来源以澄清这一点。

There are two main types of "commands" that the shell can execute. Shell可以执行两种主要的“命令”类型。 Built-in commands are executed by the shell itself - no new program is started. 内置命令由外壳程序本身执行-不启动新程序。 Simply typing echo in a shell prompt is an example of such a built-in command. 只需在shell提示符下键入echo就是这种内置命令的示例。

On the other hand, other commands execute external programs (also called binaries) - and ls is an example of this kind of command. 另一方面,其他命令执行外部程序(也称为二进制文件),而ls是此类命令的示例。

So, if you run echo in a shell, it's executed by the shell itself, but if you write a C program that performs the same action, it wil be run as an external program. 因此,如果在外壳程序中运行echo ,它是由外壳程序本身执行的,但是,如果编写执行相同操作的C程序,它将作为外部程序运行。 As a matter of fact, most Linux systems come with such a binary, located at /bin/echo . 实际上,大多数Linux系统都带有这样的二进制文件,位于/bin/echo

Why does it sometimes make sense to have both a built-in command and a program to accomplish the same task? 为什么有时同时具有内置命令和程序来完成相同任务是有意义的? Built-in commands are faster to execute as there is some cost involved in running an external program. 内置命令的执行速度更快,因为运行外部程序会涉及一些成本。 But built-ins have some drawbacks, too: they can't be too complex as this would make the shell big and slow; 但是内置函数也有一些缺点:它们不能太复杂,因为这会使外壳变大和变慢。 they can not be upgraded separately from the shell and from each other; 它们不能独立于外壳和彼此升级; finally, there are situations where an external program which is not your shell would like to run an application: it can run external programs but it can't execute shell built-ins directly since it's not the shell. 最后,在某些情况下,不是您的外壳程序的外部程序想要运行应用程序:它可以运行外部程序,但是由于它不是外壳程序,因此不能直接执行外壳程序内置程序。 So sometimes it makes sense to have it both ways. 因此有时同时兼顾两者都是有意义的。 Apart from echo , time is another example of this double approach. 除了echo之外, time是这种双重方法的另一个示例。

The shell is just a user level way of interacting with the operating system, or the kernel. Shell只是与操作系统或内核进行交互的用户级方式。 That's one of the reasons it's called a shell . 这就是它被称为shell的原因之一。 The shell itself ( sh , csh , tcsh , ksh , zsh , bash , etc...) is essentially just a binary the operating system executes to allow you to execute other binaries. Shell本身( shcshtcshkshzshbash等)本质上只是一个二进制文件,操作系统将执行该二进制文件以允许您执行其他二进制文件。

It generally gives a lot of other functionality though like built in functions ( echo , fg , jobs , etc...), an interpreted language ( for x in ... , if then , etc...), command history, and so on... 它通常提供许多其他功能,例如内置函数( echofgjobs等),解释语言( for x in ...if then等),命令历史记录和等等...

So, any text entered into the shell (like echo ), the binary (or process) interprets and runs the corresponding functions in its code. 因此,任何输入到外壳程序中的文本(如echo ),二进制文件(或进程)都会在其代码中解释并运行相应的功能。 Built in functions (like echo ) don't need to create a new process, but if the text is interpreted as a request to execute a binary ( vim , emacs , gcc , test , true , false , etc...) the shell will create a new process for it (unless you prefix it with exec ), and execute it. 内置函数(例如echo )不需要创建新进程,但是如果文本被解释为执行二进制文件( vimemacsgcctesttruefalse等)的请求,会为此创建一个新进程(除非您给它加上exec前缀)并执行它。

So, echo "Hello World! just runs code in the shell (process). A printf("Hello World!") would be in seperate binary that the shell would create a new process for ( fork ), and have the operating system execute ( exec ). 因此, echo "Hello World!只是在外壳程序(进程)中运行代码printf("Hello World!")将使用单独的二进制文件,外壳程序将为( fork )创建一个新进程,并让操作系统执行( exec )。

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

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