Linux study notes - SHELL command [Repost]

xiaoxiao2021-03-06  52

The shell is an interface between the user and the Linux operating system. There are a variety of shells in Linux, in which Bash is used by default. This chapter tells the working principle of Shell, the type of shell, the general operation of Shell and the characteristics of Bash. What is the shell of the shelllinux system as the housing of the operating system, providing users with an interface to use the operating system. It is a collective language, command interpreter, and quote from programming language. The shell is an interface program between the user and the Linux kernel. If the Linux kernel imagines into a sphere, the shell is the outer layer around the core. When commands from the shell or other programs to Linux, the core will make a corresponding response. Shell is a command language interpreter that has its own built-in shell command set, and the shell can be called by other applications in the system. The commands entered in the prompt are explained by the shell and then pass it to the Linux core. There are some commands, such as changing the working directory command CD, is included inside the shell. There are also some commands, such as copying commands CP and mobile command RM, which are separate programs existing in a directory in the file system. For users, don't have to care about an order is built within the shell or a separate program. Shell first checks if the command is internal command. If it is not checked if it is an application (here the application can be Linux itself, such as LS and RM, can also be a commercial program, such as XV, or freedom Software, such as Emacs. The shell is then looking for these applications in the search path (the search path is a list of directories that can find the executable program). If the typed command is not an internal command and does not find this executable file in the path, an error message will be displayed. If the command can be successfully found, the internal command or application will be decomposed into a system call and pass it to the Linux kernel. Another important feature of shell is that it itself is an interpreted programming language. The shell programming language supports most program elements that can be seen in advanced languages, such as functions, variables, arrays, and program control structures. The shell programming language is easy to learn, and any commands that can be typed in the prompt can be placed in an executable shell program. When the normal user is successfully logged in, a program called shell is executed. It is the shell process that provides a command line prompt. As the default value (the default shell of the TurboLinux system is Bash), the "$" prompt is used for ordinary users, and the "#" is prompt to the superuser (root). Once the Shell prompt appears, you can type the parameters you need to command the command name and command. The shell will execute these commands. If a command takes a long time to run, or generate a lot of output on the screen, you can issue an interrupt signal from the keyboard to issue an interrupt signal to interrupt it (before normal end, abort its execution). When the user is ready to end the login dialog process, you can type the logout command, the exit command, or the file end value (Press CTRL D implementation) to end the login. Let's intercrium how shell works. $ make Workmake: *** no rule to make target 'Work'. Stop. $ Note: make is the name of a command in the system, followed by command parameters. After receiving this command, the shell executes it. In this example, since the input command parameters are incorrect, the system returns the information to stop the execution of the command. In the example, the shell looks for a program called Make and performs it as a parameter with Work. Make is a program that is often used to compile a large program, which is compiled as a target as a goal.

In "make Work", the target of Make is Work. Because Make could not find a target named by Work, it gives an error message indicates that the run fails, and the user returns to the system prompt. In addition, the user will give an error message if the user can't find the program named by the command line. For example, if the user type: $ myProgbash: myprog: command not found, the user gets an error message that does not find the command. After the user is knocking the wrong command, the system generally gives such an error message. There are many types in the shell's type Linux, with the most commonly used Bourne Shell (SHELL (CSH) and Korn Shell (KSH). The three shells have advantages and disadvantages. The Bourne Shell is the starting shell for UNIX and can be used on each UNIX. Bourne Shell is quite excellent in shell programming, but doing less than other shells in handling the user's interaction. The default shell of the Linux operating system is Bourne Again Shell, which is an extension of Bourne Shell, is referred to as Bash, compatible with Bourne Shell, and increases many features on the basis of the Bourne Shell. BASH is placed in / bin / bash, it has many features that provide functions such as command completion, command editing, and command history tables, which also contains the advantages in many c shells and Korn shells, flexible and powerful programming Interface, and there is a very friendly user interface. C shell is a shell that is more programmed than the Bourne Shell, which is similar to the C language. Linux provides TCSH for people who like to use C shell. TCSH is an extension version of C shell. TCSH includes command line editing, programmable words, spell correction, historical command replacement, job control, and similar C language syntax, not only compatible with the Bash Shell, but also provides more prompt parameters than Bash Shell . The Korn Shell combines the advantages of C shell and Bourne Shell and is fully compatible with the Bourne Shell. The Linux system provides PDKSH (Extension of KSH) that supports task control, which can be hang on the command line, the background execution, wake up or terminate the program. Linux does not have a cold other shell users, as well as some popular shells such as ASH, ZSH, etc. Every shell has its use, some shells are patented, and some can be obtained from the Internet or other sources. To decide which shell to use, just read the online help of the various shells, and try it. Users determine which shell to use by the / etc / passwd file when logging in to Linux. For example: # fgrep lisa / etc / passwdlisa: x: 500: 500: TurboLinux User: / Home / Lisa: / bin / Bashshell is column each end (/ bin / bash). Since Bash is the default shell on Linux, this chapter mainly introduces Bash and its related knowledge. When the shell command command line C logs in to the Linux system, you can see a shell prompt and identify the beginning of the command line. Users can enter any commands and parameters later. For example: $ DATE II 11 23 01:34:58 CST 1999 When the user logs in, I actually entered the shell, which follows a certain syntax to explain the input command and transmit it to the system.

The first word entered in the command line must be a command name, the second word is the option or parameter of the command, and each word in the command line must be separated by spaces or TAB, the format is as follows: $ comMand option arguments1. The options and parameter options are code including one or more letters, with a minus sign in front (decimal, Linux to distinguish options and parameters), options can be used to change the type of action executed by the command. For example: $ lsmotd passwd $ This is an LS command without options, which can list all files in the current directory, only listing the names of each file without displaying other more information. $ LS -LTOTAL 2-RW-R - R - 2 WZH Book 22 APR 20 20:37 MOTD-RW-R - R - 2 WZH Book 796 APR 20 20:37 Passwd $ Add -L Option, Will list a line of lines for each file, such as data size and data last modified. Most commands are designed to accept parameters. The parameter is one or more words typed after the option in the command line, for example: $ ls -l text-rw-r - r - 2 WZH Book 22 APR 20 20:37 MOTD-RW-R - R - 2 WZH Book 796 APR 20 20:37 Passwd $ will display all files and their information in the Text directory. Some commands, such as the LS can be used, and some commands may require some minimum number of parameters. For example, the CP command requires at least two parameters, and if the number of parameters does not match the command, the shell will give an error message. For example: $ cp -i mydata newdata Note: The options in the command line are first in parameter input. 2. The command line characteristic command line is actually a text buffer that can be edited. You can edit the input text before pressing Enter. For example, using the Backspace key to delete the characters that just typed, you can delete it, you can also insert characters, so that when you enter commands, especially complex commands, if you type errors, you don't need to re-enter the entire command, just use the editing operation, Correct the error. Using the up arrow can re-display the just executed command, use this feature to repeat the previously performed command without retrofitting the command. Bash saves a list of previously typed commands, which is called command history table. Press the arrow to display each command on the command line. Similarly, pressing the arrow to move down in the command list, which can be displayed on the command line in the command line, and the user can modify and execute these commands. This feature will be discussed in detail in Section 10.4. In a command line, you can also place multiple commands, separated by the semicolons. For example: $ LS -F; CP -I MyData NewData can also enter a command in several command lines, with a backslash to continue to the next line. The CP command on the $ cp -ImyDataNewData is entered in the three rows. The starting two lines ends with the backslash, and the three lines are used as a command line. In addition to using normal characters in the special character shell in the shell, special characters with special meaning and feature can also be used. At the time of use, you should pay attention to its special meaning and scope of action. These special characters are described below. 1. Wildcard wildcard is used for pattern matching, such as file name matching, road scripping search, string lookup, etc. Commonly used wildcards are * ,? and character sequences in square brackets []. The user can include these wildcards in the file name as the command parameter, constitute a so-called "mode string" and perform mode matches during the execution process.

* Represents any string (length can be inequal), for example: "f *" matches any string of the head head. However, it should be noted that the dots (.) And slash (/) in the path before the file must be explicitly matched. For example, "*" cannot match .file, and ". *" Can match .file. ? Represents any individual characters. [] Represents a specified character range, as long as the [] location in the file name is within the [] location specified in [], then this file name matches this mode string. The character range in square brackets can be composed of a direct-given character, or consist of a starting character representing a defined range, a termination character, and an intermediate hyphen (-). For example, F [A- D] is the same as F [ABCD]. The shell will use all the file names that match the mode strings specified in the command line to form the final command, and then execute this command. Here we give the table 10-1 to explain the specific meaning of these wildcards. Table 10-1 Wildcard Meaning Example Mode Strings * The name of all files in the current directory. * Text * The name of the file containing text in all file names in the current directory. [AB-DM] * The name of all files starting with A, B, C, D, and M. [AB-DM]? All the names of all the files starting with A, B, C, D, M and the back of one character with a character. / usr / bin / ?? Directory / USR / bIN name of all files named two characters. It is particularly important to note that even characters "-" are only valid in square brackets, indicating the character range, as in square brackets, it is a normal character. And * and? Only in square brackets are wildcards, if they appear within square brackets, they have lost the ability of wildcards and become ordinary characters. For example, only one pair of brackets in the pattern "- a [*?] Abc" are wildcards, * and? All ordinary characters, so it matches the string that can only be - a * ABC and - a? ABC. Finally, some issues that need to be careful when using wildcards. Since * ,? and [] have a relatively special meaning for the shell, these characters should not appear in the normal file name. Especially in the directory name, do not appear, otherwise shell matches the endless recursive. It is also a point to pay attention to: If there is no file name that matches the specified mode string in the directory, the shell will use this mode string itself as a parameter to pass the relevant command. This may be the reason why a special character appears in the command. 2. Quotation marks are divided into three types: single quotes, double quotes and reverse numbers. * Single quotes' characters from single quotes appear as ordinary characters. After the special characters are enclosed in single quotes, they will lose their original meaning, but only as a normal character interpretation. For example: $ String = '$ PATH' $ Echo $ String $ PATH $ visible $ Maintaining its own meaning, as a normal character appears. * Double quotation marks "The characters enclosed by double quotes, except for $,, ', and" these characters are still special characters and retain their special features, and the rest are treated as ordinary characters. For $, it is used to replace this variable and $ with the value of the variables, which is the escape character. It tells the shell not to specially handle the characters behind it, only as a normal character can. It can be imagined that only four characters $,, 'and "itself are required in the double quotes," it is not added in front of it, and the shell matches it. "

转载请注明原文地址:https://www.9cbs.com/read-118043.html

New Post(0)