Get the App
SLTechnology News&Howtos  ›  Servers  › 

The method of running shell Program in linux

Shulou Source: shulou.com Published: 2022-06-03 07:53:14 10月16日 Update

Today, the editor shares with you the method of running shell programs in linux, which many people don't know very well. Today, in order to make you know more about the methods of running shell programs in linux, I have summarized the following contents for you. Let's look down together. I'm sure you'll get something.

There are three ways for linux to run a program, which are:

1. Make the file executable and run the file directly.

The chmod command modifies the permissions of the file. + x gives the file executable permissions. Just like we run the program above. But the results we see are somewhat different from what we imagined.

2. Directly call the command interpreter to execute the program.

As shown in the figure:

Since our interpreter is / bin/sh, we use the sh command interpreter to execute the program.

(the current work path has not changed)

3. Use source to execute files.

As shown in the figure:

(the current work path has changed)

Of the three ways to run a shell program, the first two are executed as follows:

(1) the parent process receives the command and finds that it is not a built-in command, so it creates a shell process like itself to execute the external command.

(2) the shell subprocess replaces itself with / bin/sh, and the sh process sets its own runtime environment variables, including the $PWD variable.

(3) the sh process executes the built-in commands cd and echo in turn. During this process, the environment variable of the sh process (child process) is changed by the cd command.

(4) when the child process is finished, it dies, and the waiting parent process wakes up and continues to accept orders.

Analysis:

The current directory (environment variable) of the parent process cannot be changed by the child process. However, a child process is not created when executing a shell script using source, but is executed directly in the parent process.

The method of running shell programs on linux is shared here, of course, not only the above and everyone's analysis methods, but the editor can ensure that its accuracy is absolutely no problem. I hope that the above content can have a certain reference value for everyone, and can be put into practice. If you like this article, you might as well share it for more people to see.

Tags: Process command program run method file variable permission environment interpreter interpretation content path process such as diagram analysis change work application of knowledge value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Redmi MySQL Huawei Xiaomi