Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Shell

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly shows you "how to use Shell", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Shell" this article.

Shell runs the program method: 1, make the file have executable authority, run directly; 2, directly call the command interpreter to execute the program; 3, use source command to execute the program.

The figure above is a complete and executable Linux Shell program, which is run using the first method above, and the results are as follows:

As you can see, the current directory has not been changed by the code in the program.

When the command line shell executes the program, it first determines whether the program has the permission to execute, then calls the Linux kernel command to create (fork) a process, and invokes the specified command in the new process. If the program file is compiled (forbidden file), the Linux kernel knows how to execute it. However, the echo.sh program file in the above figure is a text file, and the kernel does not know how to execute it. So the kernel returns "not executable format file" (not an executable file type), and when shell receives this message, it determines that the file is a script.

When shell determines that the file is a script, start a new shell to execute it. But today's Linux systems generally have several shell, through the first line of the script "#!" Tell the run shell to create a corresponding process to execute the script.

The figure above is the second way to run the shell program: call the command interpreter to execute the script.

The figure above is the third way to run the shell program: execute the script directly using the source command (also known as the dot command).

There are three types of commands that can be executed by Linux Shell: built-in commands, shell functions, and external commands.

The built-in commands are the commands contained in the shell program itself, which are integrated into the shell interpreter, such as cd, echo, and so on. When a built-in command is executed, there is no process creation and demise.

The shell function is a series of program code written in shell that can be referenced like other commands.

External commands are executable programs that are independent of shell, such as find, grep, echo.sh. When the command line shell executes an external command, it creates a replication process of the current shell to execute, so there is process creation and demise. The execution process is as follows:

The child process is exactly the same as the parent process at the initial stage of creation, but it cannot change the parameter variables of the parent process. Only built-in commands can change environment variables.

As explained above, when the shell script is executed using the first two methods, the execution process is as follows: when the parent process receives the command ". / echo.sh" or "/ bin/bash echo.sh", it finds that it is not a built-in command, so it creates a child process exactly like itself to execute the external command; the child process replaces itself with / bin/sh, and the sh process sets its own runtime environment variables. The sh process executes the built-in commands cd and echo in turn, and the environment variable is changed, but the environment variable of the parent process is not changed; the sh child process finishes execution and dies. The parent process wakes up from the waiting state and continues to receive commands.

When the shell script is executed with the source command, there is no creation or death of the child process, but is executed directly in the parent process.

The variable is actually a key-value pair, such as str= "Hello", which assigns the string value (value) "Hello" to the key (key) str. Within the scope of use of str, you can use str to refer to the "Hello" value, which is called variable substitution. Variable names in Shell begin with a letter or underscore, followed by letters, numbers, or underscores of any length. And it does not distinguish between variable types, all values are strings, key values have no length limit, but when the value is numeric, bash allows comparison and integer operations. When assigning a value to a key-value pair, there cannot be any spaces on both sides of the equal sign. If you want to use the variable name to get the value, you need to use the $symbol. When the content of the assignment contains spaces, you need to add quotation marks.

It's important to note that when you use the $symbol to get the value of a variable, it's just an abbreviated form of ${with_space}. In some contexts, using $with_space may cause errors, so you need to use the full form. When a variable does not have a $prefix, it may be the following situations: the variable is declared or assigned, the variable is unset, and the variable is export.

Variable assignment can be done using the equal sign, in the read command or in the loop header, such as for var in 1 2 3. Variable substitution enclosed in double quotes is not blocked, so it is called partial reference or weak reference, while the use of single quotes will prohibit variable substitution, and variable names will only be interpreted literally, so single quotes are called full references or strong references. In shell, the value of a variable can be a NULL, which is common and reasonable, but in arithmetic, a null variable is often regarded as 0, but this is a documented (and possibly non-portable) behavior.

There are two types of variables in Linux Shell: the visible scope of a local variable is in the code block or function, and it must be explicitly declared in local, whether it is globally visible; and the visible scope of global variables (such as environment variables) is global and does not require any modifiers.

In the figure above, a variable-related program is executed, and the results are as follows: the initial value of the variable num is 123; call the func1 () function to assign it to 321, it should be noted that the global variable is modified here; call the func2 () function to assign it to 456, where the assignment is added with the local declaration, so the local variable is modified.

The echo command is usually used to print individual parameters to standard output. The parameters are separated by a space and wrap after the output is printed. The following picture shows its escape character sequence.

The export command is usually used to set environment variables, but the environment variables set here only act on the current process, and the process will disappear after it dies; that is, the variables set by the child process with the export command cannot affect the parent process. However, if you use source to call the script, there are export commands in the script to modify the environment variables, which will affect the parent process.

The env command displays all environment variables without arguments, the-I parameter indicates no environment variable, and the-u parameter removes a variable from the environment variable. (unlike the set function, which displays all local variables, the env function displays only environment variables.)

The unset command defaults to the delete variable (the-v argument), and if you use the-f argument, it is considered a delete function.

Language type: the computer kernel (CPU) can not directly understand the high-level language (Linux Shell, Java, Python, etc.), but can only directly understand the machine language, so the high-level language must be translated into the machine language before the computer can execute the programs written in the high-level language.

There are two ways of translation: one is compile, the other is interpret, but the time of translation is different. Compiled language needs a special compilation process to compile the program into machine language files before executing the program, so that it does not have to be retranslated in the subsequent run. Interpretive languages do not need to be compiled, but are translated while the program is running, and there is usually a special interpreter

For example, in Linux Shell

/ bin/sh, bash, zsh, csh, etc.)

The differences between the two types of languages are as follows: many large and medium-sized programs are written in compiled languages (such as C _ Java, Java, Fortran), and their source code files (source code) are compiled into object code files (object code) before the machine can read and execute the object code files. The compiled language is efficient, but because it is close to the bottom of the machine, it is difficult to perform some simple operations at the upper level (for example, C++ is difficult to rename all the files in a directory in batches), while the interpretive language has a higher execution level than the compiled language, so it can easily complete some high-level operations; scripting languages (Shell, Perl, Python, awk, Ruby, etc.) are all interpretive languages.

(Python is an interpretive language, but it adopts the VM mechanism of Java language and provides compilation function. Its source code files are compiled to produce bytecode files (bytecode), which can save the time of loading modules and improve efficiency.)

The above is all the contents of the article "how to use Shell". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report