Guide |
Figuring out how Linux programs work is an important thing, and it's a solid foundation for our further development. This article will explain in detail how to run a Linux program through an example. I hope it is helpful for everyone to understand. |
First, let's start with a very simple example, test.sh:
#!/bin/sh#this is a Test.cd/tmpecho "hello,this is a test"
This is a very simple program, but the results of the operation may be surprising to you Oh!
650) this.width=650; "class=" AlignCenter size-full wp-image-64823 "src=" http://www.linuxprobe.com/wp-content/ Uploads/2017/05/%e6%8d%95%e8%8e%b7-1.png "width=" 393 "height=" 127 "style=" Height:auto;margin-left:auto; Margin-right:auto; "/>
At this point, we found that the program did not change the current directory after it was run. (Black question mark??? )
Don't worry, let's look at how the Linux program works.
Three methods of executing Linux programs
There are three ways to run a Linux program, namely:
1, the file has executable permissions, directly run the file.
The chmod command is used to modify a file's permissions. +x is the permission that makes the file executable. As we run the program above. But the results we see are a definite discrepancy from what we imagined.
2, directly invoke the command interpreter execution program.
As shown in the following:
650) this.width=650; "class=" AlignCenter size-full wp-image-64824 "src=" http://www.linuxprobe.com/wp-content/ Uploads/2017/05/%e6%8d%95%e8%8e%b7-2.png "width=" 380 "height=" 108 "style=" HEIGHT:AUTO;MARGIN-LEFT:AUTO; Margin-right:auto; "/>
Since our interpreter is/bin/sh, we use the SH command interpreter to execute the program.
We also see that the current work path has not changed. (Black question mark??? )
3. Execute the file using source.
650) this.width=650; "class=" AlignCenter size-full wp-image-64825 "src=" http://www.linuxprobe.com/wp-content/ Uploads/2017/05/%e6%8d%95%e8%8e%b7-3.png "width=" 407 "height=" 108 "style=" HEIGHT:AUTO;MARGIN-LEFT:AUTO; Margin-right:auto; "/>
Yes, here we find that the current working path has changed, (Black question mark????). But what exactly is it? Let's take three black question marks to learn more about how Linux programs perform.
The process of Linux program execution
When a command-line shell executes a program, it first determines whether the program has executable permissions. If you do not have executable permissions, you will be prompted: Permission denied (insufficient permissions), as shown in:
650) this.width=650; "class=" AlignCenter size-full wp-image-64827 "src=" http://www.linuxprobe.com/wp-content/ Uploads/2017/05/%e6%8d%95%e8%8e%b7-4.png "width=" 356 "height=" "style=" Height:auto;margin-left:auto; Margin-right:auto; "/>
In the first method, we execute the file directly, with the executable permission added first.
When the command line receives our execution command and determines that it has executable permissions, the call to the Linux kernel command creates a new process that invokes the specified command in the newly created process. But our test.sh is not a compiled file, so the Linux kernel does not know how to execute it, and then gives Shell,shell to know that it is a script, then starts a new shell process to execute. But the Linux system has a lot of shells,
650) this.width=650; "class=" AlignCenter size-full wp-image-64828 "src=" http://www.linuxprobe.com/wp-content/ Uploads/2017/05/%e6%8d%95%e8%8e%b7-5.png "width=" 358 "height=" 162 "style=" Height:auto;margin-left:auto; Margin-right:auto; "/>
The first line of our program saw the #!/bin/sh, at which point the command line enabled a new bash process to execute the program.
Program Execution Variance
Of the three ways in which we run the shell program, the first two methods perform the following procedures:
(1) The parent process receives the command, and then discovers that it is not a built-in command, and then creates a self-like S H L-l process to execute this external command
(2) This S H e L sub-process replaces itself with/bin/sh, the SH process sets its own run environment variable, which includes the $PWD variable.
(3) The SH process executes the built-in command CD and Echosequentially. In this process, the environment variables of the SH process (subprocess) are changed by the CD command.
(4) When the child process is finished, it dies, and waits for the parent process to wake up and continue to accept the command.
So we understand that the first two methods do not have the same result as we expected, the parent process's current directory (environment variable) cannot be changed by the quilt process.
However, executing a shell script with source does not create a child process, but rather executes directly in the parent process. Now the black question mark disappears, continue to refuel to write the code.
Original address: http://www.linuxprobe.com/run-linux-program.html
This article is from the "blog" blog, please be sure to keep this source http://coderhsf.blog.51cto.com/12629645/1923542
How to run a Linux program